【问题标题】:UIcollectionview repeats the last cell many times in Swift 3UIcollectionview 在 Swift 3 中多次重复最后一个单元格
【发布时间】:2017-08-01 09:19:03
【问题描述】:

我正在尝试通过填充从聊天室聊天 SDK 获得的所有消息并将它们填充到消息数组中来实现聊天日志控制器:var messages = [Message]()。因此我实现了以下功能:

func observeMessages() {

    let modelmessage = Message()

    //case of chat inside an Open Channel
    if openChannel != nil {
        SBDOpenChannel.getWithUrl((openChannel?.channelUrl)!) { (channel, error) in
            let previousMessageQuery = channel?.createPreviousMessageListQuery()
            previousMessageQuery?.loadPreviousMessages(withLimit: 100, reverse: false, completionHandler: { (messages, error) in
                for message in messages!{

                    if let usermessage = message as? SBDUserMessage {

                    modelmessage.fromId = usermessage.sender?.nickname
                    //print (modelmessage.fromId)
                    modelmessage.text = usermessage.message!
                    modelmessage.timestamp = usermessage.createdAt
                    self.messages.append(modelmessage)
                    print ("////")
                        for element in self.messages {
                            print(element.text)
                        }

                    }

                }
                DispatchQueue.main.async(execute: {

                    self.collectionView?.reloadData()
                })


            })


        }
    }

    //Case of a 1 on 1 chat
    else {

    }

}

以及以下负责填充 UICollectionView 的函数。

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.white
    collectionView?.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellId)

    setupInputComponents()
}



override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCell


    let message = self.messages[indexPath.item]

    cell.textView.text = message.text

    return cell
}

当我尝试在 messages 数组填充时打印它时,我意识到它是由当前索引的元素乘以索引中的数字来填充的,所以我得到了例如:

////
Optional("Aaa")
////
Optional("Hello")
Optional("Hello")
////
Optional("Hello World")
Optional("Hello World")
Optional("Hello World")

.
.
.
//Until I complete 16 iterations so I get

////
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")
Optional("Bye")

【问题讨论】:

  • print (self.messages) 行打印数组填充之前 的消息。完成处理程序的执行要晚得多。
  • 我修改了问题以在两个地方都显示打印语句。请再检查一遍好吗?
  • 如果新收到的消息应该替换以前的消息,您必须在完成处理程序的开头调用self.messages.removeAll()
  • @vadian,同样的问题是否仍然存在。
  • @vadian 请您再检查一下,我发现了一些新行为。

标签: arrays swift loops asynchronous uicollectionview


【解决方案1】:

我觉得重新加载您的收藏视图单元格的位置是错误的。 只需评论“self.collectionView?.reloadData()”,看看它是否改变了什么。

更新:

由于您在完成处理程序之外编写了“let modelmessage = Message()”,所有值都引用同一个实例,因此每次都会被覆盖。只需将其移动到完成处理程序中,您的问题就会得到解决。干杯

【讨论】:

  • 由于您在完成处理程序之外编写了此“let modelmessage = Message()”,因此所有值都引用同一个实例,因此每次都会被覆盖。只需将其移动到完成处理程序中,您的问题就会得到解决。干杯
猜你喜欢
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多