代码之家  ›  专栏  ›  技术社区  ›  عبداللطيف البهلال

如何将项目插入collectionView?Swift3

  •  1
  • عبداللطيف البهلال  · 技术社区  · 9 年前

    我正在用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]) 
    

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

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

    2 回复  |  直到 9 年前
        1
  •  0
  •   Andy Darwin    9 年前

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

        2
  •  -2
  •   Muhammad Ahmed Baig    9 年前

    self.tableView.SOMEMETHOD 但不存在名为“tableView”的出口。