我在TableView中有CollectionView . 一切都好,但是 . 当我想将我的单元格导航到另一个viewController时,我收到了错误
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let bookView: BookSingleController = storyboard.instantiateViewControllerWithIdentifier("BookSingle") as! BookSingleController
self.navigationController?.pushViewController(bookView, animated: true)
bookView.bookID = "\(self.books[indexPath.row].id)"
print(self.books[indexPath.row].id)
}
Xcode在self.navigationController上显示错误?.pushViewController(bookView,animated:true)行 . 这是错误描述:
Value of 'RelatedBookTableViewCell' has no member 'navigationController'
RelatedBookTableViewCell是我的自定义单元类:
class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
我的问题在哪里?
谢谢 .
3 回答
使用下面的代码,您可以获得Top viewcontroller并使用该视图控制器执行推送操作
使用此扩展程序
尝试从应用程序中的根控制器推送VC .
您将需要:
您将来自collectionView委托方法
didSelectItemAtIndexPath
的调用传递给您自己的自定义委托/协议,其中单元格显示单元格的UITableViewController是委托 . 这可以在cellForRowAtIndexPath中设置 .迅速
Custom Protocol
protocol DisplayBookDelegate { func displayBook(bookId: String) }
In the tableViewController
In the cell
bookId
objc :
[[NSNotificationCenter defaultCenter] postNotificationName:"kDisplayBookView" object:@{"bookId":"1"}];
swift
NSNotificationCenter.defaultCenter().postNotificationName("kDisplayBookView", object: ["bookId":"1"])