2016-05-27 4 views
0

Я пытаюсь заполнить collectionView следующим кодом, однако он всегда пуст. Я не могу понять, в чем проблема. Вот переменные после self.collectionView.reloadData() строки выполняется::Population UICollectionView не работает

enter image description here

import UIKit 

class SingleChatFull: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource, openUserChat { 

var chat_m = [Message]() 

@IBOutlet weak var collectionView: UICollectionView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    collectionView.backgroundColor = UIColor.whiteColor() 
    // Do any additional setup after loading the view. 
} 


func canOpenUserChat(messages: [Message]) { 
    chat_m = messages 
    dispatch_async(dispatch_get_main_queue()) { 
     self.collectionView.reloadData() 
    } 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 



func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return chat_m.count 

} 
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("message_cell" , forIndexPath: indexPath) as! DisplayMessageCollectionViewCell 

    cell.messageTextView.text = chat_m[indexPath.row].text 
    cell.backgroundColor = UIColor.blueColor() 

    return cell 
} 

ответ

1

UICollectionView должен знать гдеdataSource и delegate есть.

Добавьте эти две строки в viewDidLoad:

collectionView.dataSource = self 
collectionView.delegate = self 

В качестве альтернативы, вы можете использовать didSet наблюдатель группироваться, что функциональность вместе с collectionView декларации, которая является то, что я обычно делаю:

@IBOutlet private var collectionView: UICollectionView! { didSet { 
    collectionView.dataSource = self 
    collectionView.delegate = self 
} 
Смежные вопросы