【问题标题】:UITableView BeginUpdatesUITableView 开始更新
【发布时间】:2014-04-26 13:14:53
【问题描述】:

我的 UITableView 的 beginupdates 和 endupdates 出现了一些问题。我想使用这个函数来为我的 tableview 设置动画,而不是使用我现在使用的 reloadData。当我使用 BeginUpdates 执行此操作并抱怨此问题时,它总是会出现问题:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

所以我的 numberOfRowInSection 有问题:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    int returnInt;

    if (section == 0) {
        if ([self datePickerIsShown]){
            returnInt = 3;
        }else{
            returnInt = 2;
        }
    }else if([[[[self theNewGame] _dobbelstenen] objectAtIndex:section - 1] diceSoort] == ENUMOgen){
        returnInt = 1;
    }else{
        returnInt = [[[[[self theNewGame] _dobbelstenen] objectAtIndex:section - 1.0] optiesDobbelsteen] count] + 1.0;
    }

    return returnInt;
}

我这样称呼它:

if (indexPath.section == 0 && indexPath.row == 1) {
        self.datePickerIsShown = ! self.datePickerIsShown;
        [tableView beginUpdates];
        [tableView endUpdates];
    }

有什么问题?

亲切的问候

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您正在将表视图数据源中的行数从 2 更改为 3。

    if ([self datePickerIsShown]) {
        returnInt = 3;
    } else {
        returnInt = 2;
    }
    

    您必须更新表视图,以便表视图和表视图数据源在行数上达成一致。

    if (indexPath.section == 0 && indexPath.row == 1) {
        self.datePickerIsShown = ! self.datePickerIsShown;
        [tableView beginUpdates];
        if ([self datePickerIsShown])
            [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                             withRowAnimation:UITableViewRowAnimationAutomatic];
        else
            [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                             withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView endUpdates];
    }
    

    注意:由于您对表格视图只有 1 处更改,因此您不需要开始和结束更新。您可以只调用您需要的 1 个插入或删除。

    if (indexPath.section == 0 && indexPath.row == 1) {
        self.datePickerIsShown = ! self.datePickerIsShown;
        if ([self datePickerIsShown])
            [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                             withRowAnimation:UITableViewRowAnimationAutomatic];
        else
            [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]]
                             withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    

    【讨论】:

    • 感谢你的好例子!现在我对处理这些事情有了更多的了解:D
    【解决方案2】:

    您必须告诉表格视图您所做的更改,以便它知道如何制作动画。例如,如果您有 2 个项目并插入另一个项目,则表格视图只会知道您现在有 3 个项目。您需要告诉它在哪里您插入了该项目。为此,您可以在 beginUpdatesendUpdates 之间调用以下代码:

    – insertRowsAtIndexPaths:withRowAnimation:
    

    因此,例如,如果您在第 1 部分的末尾插入第三项,您的代码将如下所示:

    NSIndexPath *insertedIndexPath = [NSIndexPath indexPathForRow:2 inSection:1];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[insertedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
    

    为其他类型的更新、删除、移动等提供了几个相关的 API:

    – deleteRowsAtIndexPaths:withRowAnimation:
    – moveRowAtIndexPath:toIndexPath:
    – insertSections:withRowAnimation:
    – deleteSections:withRowAnimation:
    – moveSection:toSection:
    

    您可能还会发现我的TLIndexPathTools 库很有用。它会自动执行这些操作。

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多