如何删除自定义 UICollectionViewCell
Posted
技术标签:
【中文标题】如何删除自定义 UICollectionViewCell【英文标题】:How do I delete a custom UICollectionViewCell 【发布时间】:2014-07-03 11:33:07 【问题描述】:我真的被这个问题困住了。我有一个使用自定义单元格的 UICollectionView(在用户按下按钮后添加单元格)
每个单元格都有一个按钮以及一些其他不相关的控件。当点击该按钮时,我希望删除该单元格。
这是我目前的问题顺序:
用户点击 UICollectionView 下方的按钮进入 ViewController 编辑模式 用户点击一个单元格,所选单元格进入它自己的编辑模式(不是 VC 编辑模式) 用户出于某种原因点击了另一个单元格并被提醒一次只能编辑一个单元格 用户按下“确定”并决定通过按下单元格 A 内的按钮子视图来删除第一个选定的单元格。 由于用户在点击单元格 A 上的删除之前点击了单元格 B,因此 indexPathsForSelectedItems 设置为单元格 B 而不是单元格 A,导致用户不幸删除了错误的单元格。删除代码:
- (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];
// Set deleteAlert to NO as we're pretty much done with deleting
deleteAlert = NO;
// Set editedProjects to 0
editedProjects = 0;
// Set deletedProject
deletedProject = currentProject;
// Save the new objects
[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:@"myProjects"];
// Delete the associated subjects if any
[self deleteAssociatedSubjects];
// HERE
[_projectsCollectionView.collectionViewLayout invalidateLayout];
选择代码:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
// Get the tapped cell
ProjectCell *projectCell = (ProjectCell *)[collectionView cellForItemAtIndexPath:indexPath];
if (editMode == YES)
if (editedProjects == 0)
// Set deleteAlert to YES so that we don't mix this UIAlertView
// with the create project UIAlertView
deleteAlert = YES;
// Set editMode to YES for the selected cell
[projectCell editProject];
// Prepare the project cell
projectCell.projectTextField.text = projectCell.projectLabel.text;
// Set the firstProjectsViewController to YES
// indicating that the next tapped cell will be second in line
// in case the user decides to edit a new cell without closing this
// Set editedProjects to 1
editedProjects = 1;
else if (editedProjects == 1)
// Check if the tapped cell is being edited
if (projectCell.editMode == YES)
// Set deleteAlert to YES so that we don't mix this UIAlertView
// with the create project UIAlertView
deleteAlert = YES;
// Set editMode to YES for the selected cell
[projectCell editProject];
// Set editedProjects to 0
editedProjects = 0;
// Close the keyboard
[self.view endEditing:YES];
// Set editedProjects to 0 as we're closing editMode on this cell
editedProjects = 0;
else if (projectCell.editMode == NO)
// Tell the user that only 1 project is editable at a time
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Can only edit 1" message:@"You can only edit 1 project at a time" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
else
// Set the open project
MyManager *sharedManager = [MyManager sharedManager];
sharedManager.openProject = nil;
sharedManager.openProject = @"";
sharedManager.openProject = _objects[indexPath.item];
// Open the selected project and dismiss this ViewController
[self dismissViewControllerAnimated:YES completion:nil];
按钮 IBAction(单元格代码):
- (IBAction)deleteProjectAction:(id)sender
// Set the projectForDeletion in MyManager
MyManager *sharedManager = [MyManager sharedManager];
sharedManager.projectForDeletion = _projectLabel.text;
// Leave editMode so that we're ready for a new project cell if desired
[self editProject];
// Programatically select this cell
self.selected = YES;
// Tell the ProjectsViewController to delete the project
[[NSNotificationCenter defaultCenter] postNotificationName:@"deleteProject" object:self];
简而言之:如何轻松删除自定义 UICollectionViewCell?
我无法形容我的幸福和感谢帮助!
谢谢!
【问题讨论】:
【参考方案1】:使用collectionView:shouldDeselectItemAtIndexPath:
可防止用户取消选择当前处于编辑模式的项目(并且不允许多选,但您也可以使用collectionView:shouldSelectItemAtIndexPath:
,但不应选择点击的项目)。
【讨论】:
这正是我的想法,请问我将如何在我的代码中实现它?我会上传@Wain【参考方案2】:我通过在需要时备份 NSIndexPath 然后删除它来修复它。像这样:
didSelectItemAtIndexPath:
// Set the cellForDeletionIndexPath so that it won't change even though another cell is tapped
cellForDeletionIndexPath = indexPath;
由单元格类(按钮)发送的通知调用的删除代码
[_projectsCollectionView performBatchUpdates:^
// Now delete the items from the collection view.
[_projectsCollectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:cellForDeletionIndexPath]];
completion:nil];
【讨论】:
以上是关于如何删除自定义 UICollectionViewCell的主要内容,如果未能解决你的问题,请参考以下文章