2014-04-14 45 views
0

我已爲我的UICollectionView創建了垂直滾動自定義佈局。通過經典拉動將項目添加到此集合中,以按字母順序添加和顯示。 如果我添加一個以Z字母開頭的項目,它可能會放在集合的末尾,所以我希望集合自動滾動以顯示最後一個單元格。UICollectionView:在指定索引處添加項目並滾動到此項目

目前我使用此代碼:

[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必須滾動,則不顯示此動畫。可能是因爲scrollview也是動畫...

在你看來,這是在集合視圖中插入項目並滾動到這個新項目的最佳方式?我很難嘗試獲得漂亮流暢的動畫,以時尚的方式向用戶展示其新產品。

編輯-------

如果我刪除到reloadData通話scrollToItemAtIndexPath不工作方法......在uicollectionView根本不會滾動。

回答

0

如果您使用performBatchUpdate,則不必重新加載數據。從完成塊刪除[self.collectionView reloadData];,它應該工作。

+0

如果我刪除重載數據scrollToItemAtIndexPath函數不起作用。 – MatterGoal

相關問題