2016-06-13 50 views
0

我從這個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 
} 

的問題是,我看不到消息和以前的設置了。

我在做什麼錯?

+0

任何人都可以幫助我走向正確的方向? MyCustomCell如何保留父級的所有行爲和屬性?似乎我有超級類的初始化問題... – sbkl

回答

4

這是我設法使它的方式:

1-創建兩個子類:

  • JSQMessagesCollectionViewCellOutgoing

    import UIKit 
    import JSQMessagesViewController 
    
    class messageViewOutgoing: JSQMessagesCollectionViewCellOutgoing { 
    
        @IBOutlet weak var timeLabel: UILabel! 
    
        required init?(coder aDecoder: NSCoder) { 
         super.init(coder: aDecoder)   
        } 
    } 
    
  • JSQMessagesCollectionViewCellIncoming

    import UIKit 
    import JSQMessagesViewController 
    
    class messageViewIncoming: JSQMessagesCollectionViewCellIncoming { 
    
        @IBOutlet weak var timeLabel: UILabel! 
    
        required init?(coder aDecoder: NSCoder) { 
         super.init(coder: aDecoder)   
        } 
    } 
    

2-創建兩個名稱與上述兩個子類相關的xib文件。對於每個文件,添加文件所有者佔位符中相關的自定義類。

3-從Resources文件夾中存儲的JSQMessagesViewController庫中複製/粘貼每個相關類的xib模板,並將自定義標籤添加到其中。

4-鏈接的自定義標籤到新的子類以上

5-覆蓋以下功能

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

    let message = self.messages[indexPath.item] 

    if message.senderId == self.senderId { 

     let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! messageViewOutgoing 

     cell.timeLabel.text = localHourFormatter.stringFromDate(message.date) 

     return cell 

    } else { 

     let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! messageViewIncoming 

     cell.timeLabel.text = localHourFormatter.stringFromDate(message.date) 

     return cell 
    } 
} 

現在在標籤顯示在泡沫。

還有一些工作要做,以適應泡沫的大小,以適當的顯示,但這不是這個問題的目的。

讓我知道是否有任何意見/問題。

+2

盡你所能在這裏描述,但仍然在cellForRowAtIndexPath崩潰「無法將類型'JSQMessagesCollectionViewCellOutgoing'(0x10b5f3538)的值轉換爲'myApp.OutgoingMessageCell'(0x108cf9690)。」有任何想法嗎? –

+0

您是否解決了相同的問題? –

+0

致命錯誤:爲類 –

0

轉到Xcode中創建一個新文件,然後選擇可可觸摸類和子類列表中選擇

UICollectionViewCell,把你的自定義單元格名稱,如

customCellUICollectionViewCell.swift

之後去Mainstoryboard並將新的收集單元拖到您的視圖

選擇您的和Xcode中右側菜單中選擇

顯示身份檢查

和自定義類鍵入您的自定義類的名字

customCellUICollectionViewCell.swift

收藏視圖中的最後一件事

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! customCellUICollectionViewCell 
return cell 
} 
+0

我不能拖動一個新的收集單元。整個collectionView由庫創建。 – sbkl

0

我相信你只是沒有設置自定義單元格的文本。我還要守護安全

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

    guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCell", forIndexPath: indexPath) as? MyCustomCell else { 
    print("Could not get my custom cell") 
    return UICollectionViewCell() 
    } 

    let message = messages[indexPath.row] // or what ever your messages are called 

    let cell.mainLabel.text = message.text 

    return cell 
} 
0

我得到的錯誤:無法從messageViewOutgoing投射到JSQMessagesCollectionViewCellIncoming @sbkl。你有沒有得到這個錯誤,因爲我們不能從父類型轉換爲子類型?