有问题。我有一个 collectionView Controller ,并试图给“didSelectRowIndex...”一个任务来调用我的 rootViewController 的函数。我没有使用主板。应用程序是用代码编写的。
Collection View 单元格属性:
class feedCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UINavigationControllerDelegate {
var messageController: MessageController?
var specificRoom = [Room]()
....
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.messageController?.selectedRoom()
let roomView = self.specificRoom[indexPath.row]
self.messageController?.showChatConrollerFoRoom(roomView)
print("This is cell \(Int)") //<--- PRINTS AND CORRECT INT
}
...
}
我的 rootViewController 标题:MessageController
class MessageController: UITableViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
...
func showChatConrollerFoRoom(roomView: Room){
let roomLogController = ChatLogController(collectionViewLayout: UICollectionViewLayout())
roomLogController.roomView = roomView
navigationController?.pushViewController(roomLogController, animated: true)
print(12345678) //<---DOES NOT PRINT: FUNCTION NEVER CALLED :(
}
...
}
我是 collectionViews 的新手。不知道我在这里做错了什么。感谢您的帮助!
最佳答案
默认情况下,UICollectionViewController
是delegate
其collectionView
。 didSelectItemAtIndexPath
应在 UICollectionViewController
中实现方法,而不是 cell
中的方法。在您使用 UITableViewController
的情况下并添加了UICollectionView
您自己将委托(delegate)设置为 MessageController
也执行 didSelectItemAtIndexPath
那里。
如果你让你的 cell
delegate
collectionView
的,知道对于每个现有单元格,方法 didSelectItemAtIndexPath
当用户选择cell
时将被调用一次.
关于ios - Collection Cell 未调用 RootViewController 中的函数(在代码应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539511/