[英]How do I delete a custom UICollectionViewCell
我真的陷入了這個問題。 我有一個使用自定義單元格的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?
我無法形容我的幸福和感激之情!
謝謝!
使用collectionView:shouldDeselectItemAtIndexPath:
可以防止用戶取消選擇當前處於編輯模式的項目(並且通過不允許多次選擇,不應選擇被點擊的項目,盡管您也可以使用collectionView:shouldSelectItemAtIndexPath:
。
我通過在需要時備份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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.