临时删除不可编辑的单元格UITableView

问题描述:

我在tableView中实现了以下代码:

I have this code implemented in my tableView:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return NO;
    }
    return YES;
}

它可以满足我的要求,但是我想更进一步,使第0部分"在按下编辑按钮时完全消失(如果进入iOS的键盘"菜单并选择在右上角进行编辑,则动画中的前两个部分会消失).我试图暂时删除第一部分,但是当调用 [tableView reloadData]; 时,我的应用程序崩溃了:

It does what I want, but I want to go one step better and make "section 0" disappear altogether when the edit button is pressed (this effect can be seen if you go into the "keyboard" menu on iOS and select edit in the top right corner, the top two sections disappear in an animation). I have attempted to temporarily remove the first section, but my app crashes when [tableView reloadData]; is called:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tvController.editing == YES) {
        return 1;
    }else if (tvController.editing == NO) {
        return 2;
    }
    return 0;
}

此外,如果我能使代码正常工作,我认为我不会以动画结尾,我认为我的方法是错误的.感谢您的帮助!

Also, I do not think I will end up with an animation if I get that code working, I think my approach is wrong. Thanks for your help guys!

您的问题

您的一个部分比上一个部分长.

One of your sections is longer than the preceding one.

由于通过在 numberOfSectionsInTableView:中报告少了1个节来隐藏第0节,因此在编辑模式下,每个委托方法都必须调整节号.其中之一没有这样做.

Since you hide section 0 by reporting 1 less section in numberOfSectionsInTableView:, in editing mode every delegate method must adjust the section number. One of them is not doing so.

// every delegate method with a section or indexPath must adjust it when editing

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tvController.editing) section++;
    return [[customers objectAtIndex:section] count];
}

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
    int section = indexPath.section;
    if (tvController.editing) section++;

    id customer = [[customers objectAtIndex:section] indexPath.row];

    // etc
}

我的方法

UITableView

UITableView reloadSections:withRowAnimation: reloads specified sections with an animation. Call it from your setEding:animated: delegate method.

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animation];

    [self.tableView reloadSectionIndexTitles];

    self.navigationItem.hidesBackButton = editing;
}

您的代表还需要指出隐藏部分没有行或标题.

Your delegate also needs indicate that the hidden section has no rows or title.

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.editing && section == 0) {
        return 0;
    }

    return [[customers objectAtIndex:section] count];
}

- (NSString*) tableView:(UITableView*) tableView titleForHeaderInSection:(NSInteger) section
{
    if (self.editing && section == 0) {
        return nil;
    }

    [[customers objectAtIndex:section] title];
}