我真的坚持这个问题。我有一个使用自定义单元格的UICollectionView(按下用户按钮后添加单元格)
每个单元格都有一个按钮以及一些其他不相关的控件。点击该按钮后,我希望删除该单元格。
这是我目前的问题订单:
删除代码:
- (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?
我无法描述我的快乐和感激之情!
谢谢!
答案 0 :(得分:0)
使用collectionView:shouldDeselectItemAtIndexPath:
阻止用户取消选择当前处于编辑模式的项目(并且不允许多次选择,不应选择点击的项目,但您也可以使用{{1} })。
答案 1 :(得分:0)
我通过在我想要的时候备份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];