简体   繁体   中英

How to insert Item to a collectionView ? Swift3

I am creating a chatting app in Xcode 8 with Swift 3 and I am using collectionView for the messages. but I am having an issue with inserting a new item to the collection view when press send:

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

error show up ( Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)'

How to solve this issue. and is working with storyboard effecting the work with collectionView because I cannot change the cell size with :

 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))

    }

I have but a break point and it has not been reached!

You are using UIViewController and not UICollectionViewController so you don't get the collectionView property for free. You need to drag in an outlet form your storyboard and name it @IBOutlet var collectionView: UICollectionView!.

in my case i reference self.tableView.SOMEMETHOD but there is no outlet exist with name "tableView". please review your code, i think it's issue of outlet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM