14

私のiosアプリケーションでCollectionViewを使用しています。各収集セルには削除ボタンがあります。ボタンをクリックすると、セルが削除されます。削除後、その空白はセルの下に塗りつぶされます(私はCollectionViewをリロードしないで、最初からやり直したい)上からリロードせずにUICollectionViewからセルを削除

autolayoutを使って特定のセルをUICollectionViewから削除するにはどうすればよいですか?

+0

を選択した項目を削除します:あなたは、MVCのプロトコルに従ってください。モデルからデータを削除して、ビューをリロードします。より詳細な解答をするには、コードを投稿し(問題が疑われる部分のみ)、あなたが何を試してみたかを記述する必要があります。そうしないと、良い答えではなく、下線が得られます。良い答えを得るには良い質問があります。読んで[FAQ](http://stackoverflow.com/faq)は傷ついていません。 –

答えて

6

UITableviewControllerのようにUICollectionViewControllerに提供されるデリゲートメソッドはありません。 長いジェスチャ認識機能をUICollectionViewに追加することで、手動で行うことができます。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                         action:@selector(activateDeletionMode:)]; 
longPress.delegate = self; 
[collectionView addGestureRecognizer:longPress]; 

この特定のセルにlongGestureメソッドのボタンを追加します。そのボタンアクションで

- (void)activateDeletionMode:(UILongPressGestureRecognizer *)gr 
{ 
    if (gr.state == UIGestureRecognizerStateBegan) { 
     if (!isDeleteActive) { 
     NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:[gr locationInView:collectionView]]; 
     UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 
     deletedIndexpath = indexPath.row; 
     [cell addSubview:deleteButton]; 
     [deleteButton bringSubviewToFront:collectionView]; 
     } 
    } 
} 

- (void)delete:(UIButton *)sender 
{ 
    [self.arrPhotos removeObjectAtIndex:deletedIndexpath]; 
    [deleteButton removeFromSuperview]; 
    [collectionView reloadData]; 
} 

私はそれはあなたを助けることができると思います。

+0

'[deleteButton bringSubviewToFront:collectionView];'はエラーのようです。 –

30

UICollectionViewは、削除後にセルをアニメーション化して自動的に再配置します。

は、コレクションビューからの、短くて

[self.collectionView performBatchUpdates:^{ 

    NSArray *selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems]; 

    // Delete the items from the data source. 
    [self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths]; 

    // Now delete the items from the collection view. 
    [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths]; 

} completion:nil]; 



// This method is for deleting the selected images from the data source array 
-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths 
{ 
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet]; 
    for (NSIndexPath *itemPath in itemPaths) { 
     [indexSet addIndex:itemPath.row]; 
    } 
    [self.images removeObjectsAtIndexes:indexSet]; // self.images is my data source 

} 
+0

セルの削除時間は、アニメーションを遅くするか高速にするかが固定されています。 –

+0

UICollectionViewFlowLayoutをサブクラス化する必要があります。特に、finalLayoutAttributForItemAtIndexPathを実装します。 [this](http://stackoverflow.com/questions/16690831/uicollectionview-animations-insert-delete-items)をチェックしてください。 –

関連する問題