【问题标题】:How to insert Item to a collectionView ? Swift3如何将 Item 插入 collectionView ?斯威夫特3
【发布时间】:2017-07-03 21:19:10
【问题描述】:

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

    }

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

【问题讨论】:

  • 错误是指numberOfItemsInSection。这个错误是否显示在行上?您对该功能的实现是什么样的?你为什么显示sizeForItemAt
  • 你应该把它插入到msg.count

标签: ios swift3 storyboard xcode8


【解决方案1】:

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

【讨论】:

  • 谢谢。这是名字
【解决方案2】:

在我的情况下,我参考 self.tableView.SOMEMETHOD 但是没有名称为“tableView”的出口。 请检查您的代码,我认为这是出口的问题

【讨论】:

  • 我是插座的问题,我使用了插座的名称,它解决了
猜你喜欢
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 2017-07-22
  • 2017-05-26
  • 1970-01-01
相关资源
最近更新 更多