预装到Swift Array并更新UICollectionView - 正确重新加载

时间:2015-06-11 01:29:49

标签: ios swift uicollectionview

我想使用“willDisplayCell”为消息传递视图创建一个延迟加载方法 - 用户应该向上滚动,消息被提取并预先添加

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

  if messagesCollection.count >= 10 {

    var counter = 1

    if indexPath == NSIndexPath(forItem: counter, inSection: 0) {
      self.appendSpinner()              
      self.messageSkip = self.messageSkip + 10
      self.getMessages(self, skip: self.messageSkip)
    }       
  }   
}

这将触发Socket.IO事件以获取消息,这将触发

func loadMessages(messages:[Message]) {

  var reverted = messages.reverse() //reverse messages to be most recent on bottom
  self.messagesCollection = self.messagesCollection + messages.reverse()

  //so far its working as intended, all messages line up nicely
  dispatch_async(dispatch_get_main_queue()) {
    self.collectionView.reloadData()
  }
}

这会让我处于无限循环中 - 因为我将始终显示indexPath 1,使用批量更新的第二次尝试会产生相同的结果:

func loadMessages(messages:[Message]) {

  var reverted = messages.reverse()          
  self.messagesCollection = reverted+self.messagesCollection
  var reloadCells = [NSIndexPath]()

  for (index,singleMessage) in enumerate(reverted) {              
    var singleReloadCell = NSIndexPath(forItem: index, inSection: 0)
    reloadCells.append(singleReloadCell)            
  }

  println(reloadCells)          
  self.collectionView.performBatchUpdates({          
    self.collectionView.insertItemsAtIndexPaths(reloadCells as [AnyObject])}, completion: nil)              
  }
}

任何提示都将不胜感激。

0 个答案:

没有答案