ios - Collection Cell 未调用 RootViewController 中的函数(在代码应用程序中)

标签 ios swift uicollectionview

有问题。我有一个 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 的新手。不知道我在这里做错了什么。感谢您的帮助!

最佳答案

默认情况下,UICollectionViewControllerdelegatecollectionViewdidSelectItemAtIndexPath应在 UICollectionViewController 中实现方法,而不是 cell 中的方法。在您使用 UITableViewController 的情况下并添加了UICollectionView您自己将委托(delegate)设置为 MessageController也执行 didSelectItemAtIndexPath那里。

如果你让你的 cell delegate collectionView的,知道对于每个现有单元格,方法 didSelectItemAtIndexPath当用户选择cell时将被调用一次.

关于ios - Collection Cell 未调用 RootViewController 中的函数(在代码应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539511/

相关文章:

swift - UICollectionView Cell 按钮长按手势返回 self 不断变化?

ios - 向上滚动时在 UICollectionView 中保持滚动位置

objective-c - UIScrollView 无限滚动 setContentOffset

ios - 以编程方式向 ViewControllerCells 添加约束

ios - 使用以编程方式创建的 UICollectionView 时出现奇怪的绘图

objective-c - objective-C 头文件中的 weak in swift 消失了

swift - 使用 Swift 将对象附加到数组中,其中数组存储在字典中

iphone - 使用 AudioQueue 服务播放 MP3 数据缓冲区 : Prime failed (-50)

ios - ios 上的phonegap 2.0 有错误

ios - UICollectionView当前项目上剩余的新项目插入