ios - 将数据从自定义 UI 单元传递到 View Controller

标签 ios iphone swift uiviewcontroller uicollectionview

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

View Controller (仅部分):

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
    }
}

描述单元类:

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

感谢任何帮助,谢谢。

最佳答案

当您选择一个单元格并重新加载 Collection View 单元格时,您需要更改 dexEntry。

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

    let cell = collectionView.cellForItem(at: IndexPath(row: 1, section: 0) as! DescriptionCell
    cell.setPokemon(poke: poke)

    collectionView.reloadItems(at: IndexPath(row: 1, section: 0))
}

希望这有帮助。

关于ios - 将数据从自定义 UI 单元传递到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41807735/

相关文章:

ios - 如何实现一个(几乎)全 View uitableview 可以滚动到最后一个单元格并在 iOS 中显示更多元素

ios - Xcode Beta 6 上的 NSString 和 String

ios - "unexpectedly found nil while unwrapping an Optional value"当我尝试通过 AVAssetImageGenerator 获取 CGImage 时

ios - 在 SWIFT 中将图像作为 POST 发送

ios - 从 iOS 应用上传到自己的 YouTube 帐户

ios - 使用 IBMCloudAppID 授权委托(delegate)时委托(delegate)不符合协议(protocol)

iphone - iOS 6 将数据从 TableView 传递到 Detail View

iphone - 向导航栏添加标题

ios - 内容更改时 WKWebView 不调整大小

ios - 无法在 iPhone 6 中安装我的 ARKit 支持的应用程序,但苹果的演示应用程序在同一部 iPhone 上成功运行