【问题标题】:UICollectionView load items on top - load more optionUICollectionView 在顶部加载项目 - 加载更多选项
【发布时间】:2016-08-08 17:03:06
【问题描述】:

我用meteor-iOS构建消息应用程序,一切都很顺利,但我正在尝试加载更多功能,但我无法维护/保存滚动视图的位置(当我滚动到顶部时,我想加载更多项目,但滚动视图保持与插入之前相同的位置)

我的代码:

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {



    if type == NSFetchedResultsChangeType.Insert {

        self.blockOperations.append(
            NSBlockOperation(block: { [weak self] in
                if let this = self {

                    this.collectionView!.insertItemsAtIndexPaths([newIndexPath!])

                }
                })
        )
    }
    else if type == NSFetchedResultsChangeType.Update {
        self.blockOperations.append(
            NSBlockOperation(block: { [weak self] in
                if let this = self {


                    this.collectionView!.reloadItemsAtIndexPaths([indexPath!])
                }
                })
        )
    }
    else if type == NSFetchedResultsChangeType.Move {

        self.blockOperations.append(
            NSBlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.moveItemAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
                }
                })
        )
    }
    else if type == NSFetchedResultsChangeType.Delete {

        self.blockOperations.append(
            NSBlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.deleteItemsAtIndexPaths([indexPath!])
                }
                })
        )
    }



}

 self.collectionView!.performBatchUpdates({ () -> Void in
            for operation: NSBlockOperation in self.blockOperations {
                operation.start()
            }
            }, completion: { (finished) -> Void in
                self.blockOperations.removeAll(keepCapacity: false)


        })

我正在使用核心数据和meteor-ios。那些谁知道我能做什么?我还尝试了how to set UITableView's scroll position to previous location when click "load earlier items" button 任何其他方法都没有成功:(

非常感谢!!

【问题讨论】:

  • 嗨,你解决了吗?

标签: ios swift uicollectionview


【解决方案1】:

是的,我在 contentOffset.y 范围内做了 20-50 之间的 if 语句。如果它在这些值之间,则调用 loadMore() 一次。

使 bool 值保存 isLoadFinish 并等待单元格更新。如果您不这样做, loadMore() 将调用多次。

对于 UX 体验,您必须在调用 loadMore() 之前保存旧的 contentOffset.y 并在加载后将其重置。

我需要对其进行很多改进,现在只是简单的 hack。

祝你好运。

【讨论】:

  • 给我们一些可解释的代码,我和你一样尝试过,但不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多