将数据从自定义UI单元传递到视图控制器

时间:2017-01-23 13:39:17

标签: ios iphone swift uiviewcontroller uicollectionview

我正在创建一个pokedex应用程序,我希望它的工作方式基本上是屏幕顶部有一个滚动条,允许您选择任何口袋妖怪,并在选择口袋妖怪时,在滚动条下方,口袋妖怪的条目将出现(bulbasaur将默认存在,直到选择了一个口袋妖怪,因为bulbasaur是第一个ID为1的口袋妖怪)。为了实现这一点,我让我的视图控制器返回两种类型的单元格,第一种是“选择器单元”,它是滚动器,第二种是“描述单元”,它是dex条目。我给视图控制器一个名为dex entry的数据成员,并在cellForItemAt函数中返回dex条目,但是单元格的图像没有变化(从bulbasaur到我选择的哪个口袋妖怪)。我打印到控制台每次选择一个神奇宝贝时,dex条目的口袋妖怪的价值是什么,所以我确信dex条目正在直接改变,但我不知道为什么图像也没有改变。以下是我的代码的相关部分。

查看控制器(仅部分):

import UIKit

class PokeDexController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var dexEntry = DescriptionCell()

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "PokeDex 386"
    collectionView?.backgroundColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 64/255.0, alpha: 1.0)
    //collectionView?.backgroundColor = UIColor.white

    collectionView?.register(chooserCell.self, forCellWithReuseIdentifier: cellID)
    collectionView?.register(DescriptionCell.self, forCellWithReuseIdentifier: descID)

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if (indexPath.row == 0)
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! chooserCell
        return cell
    }
    else{
        let descCell = collectionView.dequeueReusableCell(withReuseIdentifier: descID, for: indexPath) as! DescriptionCell
        dexEntry = descCell
        return dexEntry
    }
}

descriptionCell class:

import UIKit

class DescriptionCell: UICollectionViewCell
{
    private var pokemon : Pokemon?
    {
    didSet
    {
        if let id = pokemon?._id
        {
            imageView.image = UIImage(named: String(id))
            print("Pokemon with the id of " + String(id))
        }
    }
}

override init(frame: CGRect)
{
    super.init(frame: frame)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setPokemon(poke: Pokemon)
{
    self.pokemon = poke
}

func getPokemon() -> Pokemon
{
    return pokemon!
}

let imageView: UIImageView =
    {
        let iv = UIImageView()

        iv.image = UIImage(named: "1")
        iv.contentMode = .scaleAspectFill

        return iv
}()

func setupViews()
{
    backgroundColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 64/255.0, alpha: 1.0)
    addSubview(imageView)

    imageView.frame = (CGRect(x: frame.width/6, y: frame.height/30, width: frame.width/4, height: frame.height/4))
}

}

choosercell类(特别是didSelectItemAt):

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
    let poke = pokemon[indexPath.row]
    print("Selected " + poke._name)

    let vc = PokeDexController()
    vc.dexEntry.setPokemon(poke: poke)

    let name = vc.dexEntry.getPokemon()._name
    print(name ?? "nothing there")
}

image of the app and the console output

感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

我还没有解决我的问题但是我意识到我在viewController中返回的单元格与dexEntry无关,所以就我可以单元格而言,一旦设置了该单元格,它就被设置了,所以我现在我将弄清楚如何在选择单元格时重新加载东西,以便返回的单元格具有不同的小精灵图像。

答案 1 :(得分:0)

选择单元格并重新加载集合视图单元格时,需要更改dexEntry。

String searchString = "word1* AND word2*";
QueryParser queryParser = new QueryParser(Version.LUCENE_46,"content", analyzer);
Query query = queryParser.parse(searchString);

希望这有帮助。