如何将 Item 插入到 collectionView 中?斯威夫特3

How to insert Item to a collectionView ? Swift3

我正在 Xcode 8 和 Swift 3 中创建一个聊天应用程序,我正在使用 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))

    }

我只有一个断点,还没有达到!

就我而言,我参考 self.tableView.SOMEMETHOD 但不存在名称为 "tableView" 的出口。 请检查您的代码,我认为这是 outlet

的问题

您使用的是 UIViewController 而不是 UICollectionViewController,因此您无法免费获得 collectionView 属性。您需要从情节提要中拖入一个插座并将其命名为@IBOutlet var collectionView: UICollectionView!.