0

我有一個水平UICollectionView實施UICollectionViewLayout並使用targetContentOffsetForProposedContentOffset自定義的投擲/尋呼功能。每個單元格幾乎是屏幕的寬度,下一個單元格顯示在右側。該功能很好,但我希望能夠刪除位於索引0的單元格,當前單元格是索引1.我目前能夠計算並做到這一點,但是在刪除索引0單元格後,它會滑動到下一個單元格(舊索引2,新1(刪除0之後)),因爲當前的內容偏移。我不知道如何在保持當前佈局的同時刪除索引0。UICollectionview自定義流佈局刪除單元格

現在我委託我做:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 

float cellWidth = self.collectionView.bounds.size.width - 20; 

float currentPage = self.collectionView.contentOffset.x/cellWidth; 


if (currentPage == 1) { 
    [self remove:0]; 
} 


} 


-(void)remove:(int)i { 

    [self.collectionView performBatchUpdates:^{ 
     [self.data removeObjectAtIndex:0]; 

     NSIndexPath *indexPath =[NSIndexPath indexPathForRow:0 inSection:0]; 
     [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 



    } completion:^(BOOL finished) { 


    }]; 
} 

所以計算和刪除工作正常,但在正在刪除[0],集合視圖滾動到下一cell..and我我不知道如何阻止它。我試過self.collectionview.contentOffset = CGMakePoint(0,0)刪除後,但過渡仍然明顯錯誤。關於如何解決這個問題的任何想法?

回答

0

因此,經過大量的試驗和錯誤,我能夠解決問題。這可能有點駭人聽聞,但我只用一個沒有持續時間的uiview動畫來包裝批處理。基本上它覆蓋了刪除動畫

[UIView animateWithDuration:0 animations:^{ 
    [self.collectionView performBatchUpdates:^{ 
     [scrollView setContentOffset:CGPointMake(0, 0) animated:NO]; 
     [self.data removeObjectAtIndex:0]; 
     NSIndexPath *indexPath =[NSIndexPath indexPathForRow:0 inSection:0]; 
     [self.collectionView deleteItemsAtIndexPaths:@[indexPath]]; 
     [self.collectionView.collectionViewLayout invalidateLayout]; 
    } completion:nil]; 
}]; 
相關問題