我已经把一个uicollectionview放入了一个uitableview。在选择表视图单元格内的collectionview单元格后,我遇到了另一个viewcontroller的麻烦。从uicollectionview中查看是在桌面视图内
// if the user selects a cell, navigate to the viewcontroller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// we check did cell exists or did we pressed a cell
if let cell = sender as? UICollectionViewCell {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell") as! TestingTableView
// define index to later on pass exact guest user related info
let index = cell2.collectionView?.indexPath(for: cell)!.row
print(index as Any)
// if segue is guest...
if segue.identifier == "guest" {
// call guestvc to access guest var
let guestvc = segue.destination as! GuestCommunityViewVC
// assign guest user inf to guest var
guestvc.guest = communities[index!] as! NSDictionary
}
}
}
}
我在该行收到一个错误:
let index = cell2.collectionView?.indexPath(for: cell)!.row
,因为跟它的价值为零。有没有人知道一个更好的方法来做到这一点?
在上面的错误提示行中,您将出现一个单元格并将其转换为TestingTableView对象。你的意思是把这个当作一个TableViewCell吗? – bjd23
@ bjd23我的集合视图插座是在表视图单元类中声明的,因为它嵌套在表视图中。这是我可以访问collectionview的索引路径的唯一方法。但是,如果它返回nil,我认为它甚至不会获得collectionview的indexpath。我一直在为此奋斗一段时间。有什么建议么? – ILoveToCode22
为什么不使用委托,所以当按下UICollectionViewCell时,它会将此内容发送给UITableView所在的ViewController? – Jay