簡體   English   中英

如何將Item插入到collectionView中? 迅捷3

[英]How to insert Item to a collectionView ? Swift3

我正在使用Swift 3在Xcode 8中創建一個聊天應用程序,並且正在使用collectionView作為消息。 但在按發送時將新項目插入到收藏夾視圖時遇到問題:

msg.append(message) // msg : Messages Array    
let item = msg.count - 1 
let IndexPath = NSIndexPath(item: item, section: 0)
self.collectionView.insertItems(at: [IndexPath]) 

顯示錯誤(對成員'collectionView(_:numberOfItemsInSection :)的引用不明確”

如何解決這個問題。 並且正在與情節提要一起工作,從而實現了collectionView的工作,因為我無法使用以下方式更改單元格的大小:

 public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if let messageText = msg[indexPath.item].text {
            let size = CGSize(width: 250 , height: 1000)
            let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
            let esf = NSString(string: messageText).boundingRect(with: size, options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(18))], context: nil)
            print("Changed")
            return CGSize(dictionaryRepresentation: esf as! CFDictionary)!
        }

          print("Not Changed")
        return CGSize(width: view.frame.width, height: CGFloat(100))

    }

我只有一個斷點,尚未到達!

您使用的是UIViewController而不是UICollectionViewController,因此不會免費獲取collectionView屬性。 您需要從情節提要中拖出插座,並將其命名為@IBOutlet var collectionView:UICollectionView!。

在我的情況下,我引用self.tableView.SOMEMETHOD但沒有名稱為“ tableView”的插座。 請檢查您的代碼,我認為這是插座的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM