简体   繁体   English

如何删除自定义UICollectionViewCell

[英]How do I delete a custom UICollectionViewCell

I'm really stuck at this one problem. 我真的陷入了这个问题。 I have a UICollectionView that uses a custom cell (cells are added after user's button press) 我有一个使用自定义单元格的UICollectionView(在用户按下按钮后添加了单元格)

Every cell has a button alongside with a few other unrelated controls. 每个单元格都有一个按钮以及一些其他不相关的控件。 When that button is tapped, I want the cell to be deleted. 点击该按钮后,我希望删除该单元格。

This is my current problem order: 这是我当前的问题顺序:

  • User taps a button beneath UICollectionView to enter the ViewController editMode 用户点击UICollectionView下面的按钮以进入ViewController editMode
  • User taps a cell and the selected cell enters it's own editMode (not VC editMode) 用户点击一个单元格,然后所选单元格进入它自己的editMode(不是VC editMode)
  • User for some reason taps on another cell and gets reminded that only 1 cell is editable at a time 用户出于某种原因轻敲另一个单元格并得到提醒,一次只能编辑一个单元格
  • user presses "ok" and decides to delete the first selected cell by pressing a button subview inside cell A. 用户按下“确定”并决定通过按下单元格A内的按钮子视图来删除第一个选定的单元格。
  • because the user tapped cell B before tapping delete on cell A, the indexPathsForSelectedItems is set to cell B instead of Cell A, causing the user to unfortunately delete the wrong cell. 因为用户在单元格A上点击“删除”之前轻按了单元格B,所以indexPathsForSelectedItems设置为单元格B而不是单元格A,从而导致用户不幸地删除了错误的单元格。

Deletion code: 删除码:

- (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];
}

Select code: 选择代码:

- (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];
}
}

Button IBAction (cell code): 按钮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];
}

In short: how do I delete a custom UICollectionViewCell easily? 简而言之:如何轻松删除自定义UICollectionViewCell?

I can't describe my happiness and gratitude for help! 我无法形容我的幸福和感激之情!

THANKS! 谢谢!

使用collectionView:shouldDeselectItemAtIndexPath:可以防止用户取消选择当前处于编辑模式的项目(并且通过不允许多次选择,不应选择被点击的项目,尽管您也可以使用collectionView:shouldSelectItemAtIndexPath:

I fixed it by backing up the NSIndexPath when I wanted to and then deleting that. 我通过在需要时备份NSIndexPath并将其删除来修复它。 Like this: 像这样:

didSelectItemAtIndexPath: didSelectItemAtIndexPath:

    // Set the cellForDeletionIndexPath so that it won't change even though another cell is tapped
    cellForDeletionIndexPath = indexPath;

Deletion code called by a notification sent from cell class (button) 从单元格类发送的通知所调用的删除代码(按钮)

    [_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM