我有一个 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/