22

コレクションビューを持っていて、didSelectメソッドでコレクションビューからセルを削除しようとしました。以下の方法で成功しました

  [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

しかし今、CollectionView Cell.Here からボタン クリックで項目を削除する必要があります。ここでは、indexpath.row のみを取得します。これからアイテムを削除できません。私はこのようにしてみました。

-(void)remove:(int)i {

    NSLog(@"index path%d",i);
   [array removeObjectAtIndex:i];

   NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
   [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    [colleVIew reloadData];
  }

ただし、CollectionView をリロードする必要があるため、削除後のセル配置のアニメーションはありません。アイデアを提案してください..事前に感謝します

4

7 に答える 7

21

Swift 3 ソリューション:

func remove(_ i: Int) {

    myObjectsList.remove(at: i)

    let indexPath = IndexPath(row: i, section: 0)

    self.collectionView.performBatchUpdates({
        self.collectionView.deleteItems(at: [indexPath])
    }) { (finished) in
        self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
    }

}

および削除呼び出しの例:

self.remove(indexPath.row)
于 2015-08-17T09:12:56.297 に答える
4
[array removeObjectAtIndex:[indexPath row]];
    [collection reloadData]; // Collection is UICollectionView

これを試して。

于 2013-04-30T09:36:10.390 に答える
0

答えが出ました..

CollectionViewCell にボタンを作成します // removeBtn という名前を付けました

次に CollectionView Delegate で

 - cellForItemAtIndexPath

   [cell.removeBtn addTarget:self action:@selector(RemovePrssd:) forControlEvents:UIControlEventTouchUpInside];

次に、メソッドを追加します

-(void)RemovePrssd:(id)sender{

 UIView *senderButton = (UIView*) sender;
 NSIndexPath *indexPath = [colleVIew indexPathForCell: (UICollectionViewCell *)[[senderButton superview]superview]];

  [array removeObjectAtIndex:indexPath.row];
  [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
  }
于 2013-04-30T09:59:56.530 に答える