如何删除自定义UICollectionViewCell

时间:2014-07-03 11:33:08

标签: ios objective-c uicollectionview uicollectionviewcell

我真的坚持这个问题。我有一个使用自定义单元格的UICollectionView(按下用户按钮后添加单元格)

每个单元格都有一个按钮以及一些其他不相关的控件。点击该按钮后,我希望删除该单元格。

这是我目前的问题订单:

  • 用户点击UICollectionView下方的按钮进入ViewController editMode
  • 用户点按一个单元格,所选单元格输入自己的editMode(不是VC editMode)
  • 用户出于某种原因点击另一个小区并被提醒一次只能编辑一个小区
  • 用户按“确定”并决定按小区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?

我无法描述我的快乐和感激之情!

谢谢!

2 个答案:

答案 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];