2017-01-09 7 views
0

に問題があります。私は、collectionViewコントローラを持っていて、 "didSelectRowIndex ..."に私のrootViewControllerの関数を呼び出すタスクを与えようとしています。私はMAINSTORYBOARDを使用していません。アプリケーションはコードで書かれています。コレクションRootViewController(コードアプリケーションで)でセルが呼び出されない関数

コレクションビューのセルプロパティ:

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に新しいです。私がここで間違っていることを確かめない。あなたの助けに感謝します!

答えて

0

デフォルトでは、UICollectionViewControllercollectionViewdelegateです。 didSelectItemAtIndexPathは、UICollectionViewControllerメソッドで実装する必要がありますが、cellでは実装しないでください。 UITableViewControllerを使用し、自分でUICollectionViewを追加した場合は、代理人をMessageControllerに設定してdidSelectItemAtIndexPathを実装してください。

そして、あなたはcollectionViewのごcelldelegateを加えた場合、ユーザーはcellを選択したときに、すべての既存のセルのために、方法didSelectItemAtIndexPathは一度呼び出しされることを知っています。

+0

です。しかし、各セルにはまだ一意のindexPathがありますか?私はまだ少し混乱しています。私は今、 "... viewController"には、collectionViewの代理人が含まれていることを理解しています。しかし、私はcollectionViiewを** [怠惰なvar collectionView:UICollectionView = {let cv = UICollectionView()、cv.dataSource = Self; cv.delegate =セルフリターンcv}()** – Ketone

+0

はい、メソッド 'didSelectItemAtIndexPath'では、各セルでindexPathが一意になります。そのindexPathにセルからアクセスする必要がある場合は、代わりにindexPathプロパティをセルをデキューするときに更新されるセル。しかし、セルに値を設定するだけであれば、indexPathはメソッドが現在どのセルにポピュレートしようとしているかを十分に伝えます。 –

関連する問題