简体   繁体   English

Swift UICollectionViewController 与 UICollectionViewCell 内的参数

[英]Swift UICollectionViewController with parameter inside UICollectionViewCell

I'm configuring my cell like this:我正在像这样配置我的单元格:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId for: indexPath) as! MyCollectionViewCell
    cell.index = indices[indexPath.item]
    return cell
}

And then inside my cell, I have another UICollectionViewController that has some property, say - index然后在我的单元格内,我有另一个 UICollectionViewController 有一些属性,比如说 - 索引

class MyCollectionViewCell: UICollectionViewCell {       
    var index: Int = 0 
    var myController = UINavigationController(rootViewController: anotherCollectionViewController(index: self.index ?? 0))

    override init(frame: CGRect) {
        super.init(frame: frame)
        // setting constraints, adding views etc
        addSubview(anotherCollectionViewController.view)
        ....
    }
}

The issue is when I initialize cells, they are created with default index thus anotherCollectionViewController has a default index as well.问题是当我初始化单元格时,它们是使用默认索引创建的,因此 anotherCollectionViewController 也具有默认索引。 How do I make it 'wait' for my index?如何让它“等待”我的索引?

Better give alternate name to one of them.最好给其中一个人提供替代名称。

You can use didSet function to init inner view controller, didSet is called immediately after the new value is stored:您可以使用didSet function 来初始化内部视图 controller,在存储新值后立即调用didSet

class MyCollectionViewCell: UICollectionViewCell {
    var index: Int? {
        didSet {
            if let index = index {
                myController = UINavigationController(rootViewController: anotherCollectionViewController(index: index))
            }
        }
    }

    var myController: UINavigationController? {
        didSet {
            // remove old view controller from hierarchy
            // oldValue?.removeFromParent()
            // setting constraints, adding views etc
        }

    }

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

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

    override func prepareForReuse() {
        index = nil
        myController = nil
        super.prepareForReuse()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 4:将UICollectionViewCell中的字符串传递给UICollectionViewController - Swift 4: Passing a string in UICollectionViewCell to UICollectionViewController Swift 1.2 UICollectionViewController if语句与UICollectionViewCell图像 - Swift 1.2 UICollectionViewController if statement with UICollectionViewCell Image SWIFT 2-无法将值从UICollectionViewController传递到UICollectionViewCell。 发现NIL - SWIFT 2 - Impossibile to pass value from UICollectionViewController to UICollectionViewCell. Found NIL 在UICollectionViewController中按下UICollectionViewCell之后,没有响应 - No responed after pressing UICollectionViewCell in the UICollectionViewController UICollectionViewCell的UiTextField,在UICollectionViewController中重叠 - UICollectionViewCell's UiTextField, overlapping in UICollectionViewController UICollectionViewController不显示自定义UICollectionViewCell的子视图 - UICollectionViewController not showing subviews of custom UICollectionViewCell 如何在 UICollectionViewCell 内的 UITableView 之间同步滚动(在 UICollectionViewController 内) - How can I synchronise the scrolling between a UITableView inside of a UICollectionViewCell (Inside a UICollectionViewController) 在另一个UICollectionViewController中添加UICollectionViewController - Adding a UICollectionViewController inside another UICollectionViewController 根据JSON Swift中的参数自动排序UICollectionViewCell - Auto Sort UICollectionViewCell based on parameter in JSON swift Swift-在UITableViewCell内为UICollectionViewCell设置高度 - Swift - set height for UICollectionViewCell inside UITableViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM