Удалить ячейку из UICollectionView
Я использую CollectionView в своем приложении iphone. Каждая ячейка коллекции содержит кнопку удаления. Нажав на кнопку, ячейка должна быть удалена. После удаления это пространство будет заполнено нижней ячейкой (я не хочу перезагружать CollectionView и начинать с вершины снова)
Как удалить определенную ячейку из UICollectionview с автозапуском?
Ответы
Ответ 1
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
}
Ответ 2
Никакие методы делегирования не предоставлены UICollectionViewController, как UITableviewController.
Мы можем сделать это вручную, добавив в 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];
}
Я думаю, это может вам помочь.