我從這個link,您可以通過重寫此方法定製JSQMessagesViewController庫的細胞理解:斯威夫特 - JSQMessagesViewController - 細胞自
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//Configure your cell here
return //yourCustomCell
}
是否有可能獲得的樣本代碼來了解如何實現實際的定製包括JSQMessagesCollectionViewCell的子類?
作爲一個例子,我想添加一個包含在消息氣泡底部的標籤,顯示消息發送的時間。
謝謝。
編輯
我創建了一個自定義單元格類如下:
class MyCustomCell: JSQMessagesCollectionViewCell {
var textLabel: UILabel
override init(frame: CGRect) {
self.textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height/3))
super.init(frame: frame)
self.textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
self.textLabel.textAlignment = .Center
contentView.addSubview(self.textLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
定製單元編程,而不任何XIB文件。
所以在我JSQMessagesViewController,我必須把它聲明如下:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "MyCustomCell")
}
然後,我能夠覆蓋cellForItemAtIndexPath方法如下:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomCell
return cell
}
的問題是,我看不到消息和以前的設置了。
我在做什麼錯?
任何人都可以幫助我走向正確的方向? MyCustomCell如何保留父級的所有行爲和屬性?似乎我有超級類的初始化問題... – sbkl