ios - 如何从嵌套的 collectionView 单元格开始?

标签 ios swift uicollectionview segue uistoryboardsegue

我有一个 collectionView 用于在页面之间滚动,在其中一个整页单元格中我有另一个带有单元格的 collectionView 。当最里面的 collectionView 中的一个单元格被点击时,如何执行 segue。

最佳答案

您将需要对具有 Collection View 的单元格进行委托(delegate),在选择特定单元格时需要通知它:

protocol CollectionCellDelegate: class {
    func selectedItem()
}

class CollectionCell: UITableViewCell {

    weak var delegate: CollectionCellDelegate?

    // ...

    func collectionView(_ collectionView: UICollectionView,
                    didSelectItemAt indexPath: IndexPath) {
        self.delegate?.selectedItem()
    }
}

并且在 TableViewController 中,您将必须实现该委托(delegate)以从它执行 segue(您必须从 UIViewController 子类执行 segue,但是 UITableViewCell 不是子类化它,这就是为什么你需要委托(delegate)模式)。

class TableViewController: UITableViewController, CollectionCellDelegate {

    // ...

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        // set its delegate to self
        cell.delegate = self
        return cell
    }

    func selectedItem() {
        // here you can perform segue
        performSegue(withIdentifier: "mySegue", sender: self)
    }

}

我没有向委托(delegate)传递任何参数,但您当然可以使用参数来传递转场所需的任何信息(例如,所选集合单元格的 ID 等)。

关于ios - 如何从嵌套的 collectionView 单元格开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48604717/

相关文章:

ios - xcode 4.6问题/metaio配置中的“文件所有者”

ios - 如何在 SpriteKit 中为关节添加纹理?

ios - 如何将字符串保存到数组并在标签中显示字符串?

ios - 不能分配给任何对象类型的不可变表达式

swift - 将图像从 documentDirectory 加载到 UICollectionView - 滚动时滞后

objective-c - 如何在 ReactiveCocoa 5 中将 RACSignal 转换为 SignalProducer?

ios - 如何在 UIViewController 中对 UICollectionView 进行单元测试

swift - 如何将字段添加到 Realm 数据库的架构中?

ios - 如何在选择时展开 UICollectionView 单元格而不弄乱 Collection View

ios - UICollectionview 不起作用,我没有收到任何 Cell