Collection Cell不在RootViewController中调用函数(在代码应用程序中)

时间:2017-01-09 00:41:52

标签: ios swift uicollectionview

有问题。我有一个collectionView控制器,我试图给“didSelectRowIndex ...”一个任务来调用我的rootViewController函数。我没有使用MAINSTORYBOARD。应用程序是用代码编写的。

集合查看单元格属性:

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

1 个答案:

答案 0 :(得分:0)

默认情况下,UICollectionViewController是其delegate的{​​{1}}。 collectionView应在didSelectItemAtIndexPath方法中实施,而不是在UICollectionViewController范围内实施。在您使用cell并自己添加UITableViewController的情况下,请将代理设置为UICollectionView并在那里实施MessageController

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