【问题标题】:UICollectionView: Adding item at a specified Index and scroll to this itemUICollectionView:在指定索引处添加项目并滚动到该项目
【发布时间】:2014-04-14 13:38:25
【问题描述】:

我为UICollectionView 创建了一个垂直滚动自定义布局。通过经典的拉式添加项目将项目添加到此集合中,并按字母顺序显示。 如果我添加一个以 Z 字母开头的项目,它可能会放在集合的末尾,所以我希望集合自动滚动以显示最后一个单元格。

目前我正在使用此代码:

 [self.collectionView performBatchUpdates:^{

            // Update the datasource
            [self updateTasks]; 

            // Insert the new item into the collection
            [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:[self.tasks indexOfObject:task] inSection:0]]];

        } completion:^(BOOL finished) {

            // reload data and scroll 
            [self.collectionView reloadData];
            [self.collectionView scrollToItemAtIndexPath:@[[NSIndexPath indexPathForItem:[self.tasks indexOfObject:task] inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

        }];

它有效...但正如您所见,我使用performBatchUpdates 添加我的项目。因此UICollectionView 会自动设置动画以呈现新单元格并移动其他单元格。 如果UICollectionView 必须滚动,则不会显示此动画。可能是因为滚动视图也在动画......

您认为在集合视图中插入一个项目并滚动到这个新项目的最佳方式是什么?我很难获得漂亮流畅的动画,以时尚的方式向用户展示它的新项目。

编辑------

如果我删除对reloadData 的调用,scrollToItemAtIndexPath 方法将不起作用...... uicollectionView 根本不会滚动。

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    如果您使用 performBatchUpdate,则不必重新加载数据。从完成块中删除[self.collectionView reloadData];,它应该可以工作。

    【讨论】:

    • 如果我删除重载数据,scrollToItemAtIndexPath 函数不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多