删除UICollectionViewCell

时间:2014-06-18 22:12:30

标签: ios objective-c uicollectionview uicollectionviewcell collectionview

我正在使用以下代码删除CollectionViewCell:

- (void)deleteProjects:(NSNotification *)notification {
// Get the current project
NSString *currentProject = [[MyManager sharedManager] projectForDeletion];

[_objects removeObject:currentProject];

[_projectsCollectionView performBatchUpdates:^{

    NSArray *selectedItemsIndexPaths = [_projectsCollectionView indexPathsForSelectedItems];

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

} completion:nil];

// Subtract 1 from editedProjects
editedProjects = editedProjects - 1;

// Set deletedProject
deletedProject = currentProject;

// Delete the associated subjects if any
[self deleteAssociatedSubjects];

// Save the new objects
[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:@"myProjects"];
}

供用户删除项目:

  • 进入编辑模式
  • 选择单元格
  • 选择一个会出现
  • 的红色x
  • 它会被删除

现在,如果用户选择2个单元格,他/她将收到通知,一次只能编辑1个 - 但项目仍然被“选中” - 导致删除删除无法编辑的项目而不是原来的。我该如何解决这个问题?

选择方法:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:     (NSIndexPath *)indexPath
{
}

谢谢!

1 个答案:

答案 0 :(得分:0)

indexPathForSelectedItem看起来很麻烦。

你可以这样做来获取indexPath:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[test indexOfObject:currentProject] inSection:0];

[_objects removeObject:currentProject];

[_projectsCollectionView performBatchUpdates:^{

    [_projectsCollectionView deleteItemsAtIndexPaths:@[indexPath]];

} completion:nil];

...