所以我正在尝试从嵌入在集合视图单元格中的UITableViewCell推送UITableViewController。
因此,结构是UICollectionView> UICollectionViewCell> UITableView> UITableViewCell。
是否要在单击表视图单元格时调用 segue?
我该怎么做,因为您无法访问函数执行标识符的 segue?
您可以为此创建一个protocol
,并使用具有collectionView
的ViewController实现它,然后在collectionViewCell
中将该协议的实例属性cellForItemAt
并在方法中设置该属性。现在,在tableView
的didSelectRowAt
方法中,使用要传递 segue 的详细信息调用委托方法。
protocol PassData {
func performSegue(with data: String) //Set argument type to Type that you want pass instead of String
}
现在用你的ViewController
实现这个PassData
协议,并在cellForItemAt
内设置UICollectionViewCell
的委托。
class ViewController: UIViewController, PassData, UICollectionViewDelegate, UICollectionViewDataSource {
//Other methods
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
cell.passDelegate = self
return cell
}
//Add the `performSegue(with:)` delegate method of PassData
func performSegue(with data: String) {
//Perform segue here
self.performSegue(withIdentifier: "SegueIdentifier", sender: data)
}
}
现在,在您的CustomCollectionViewCell
中,在 tableView 的方法中didSelectRowAt
调用其委托方法后,将一个名为 PassData?
类型的 passDelegate
的实例属性
class CustomCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
var passDelegate: PassData?
//Your other methods
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Pass whatever data that you want to pass
self.passDelegate?.performSegue(with: array[indexPath.row])
}
}
可以通过从表视图委托方法调用 self.navigationController?.pushViewController(tableviewVC, animate: true)
将表视图控制器推送到导航堆栈didSelectRowAtIndexPath