展开/折叠UITableView中的单元格数组

时间:2016-08-17 00:59:28

标签: ios objective-c uitableview

我想通过单击单元格上的按钮来扩展和折叠我的uitableview中的单元格数组。我不想扩展或折叠我点击的单元格,但后续的子单元格。另外,我想扩展和折叠数组中的那些项目。

目前,我有以下代码,但它不会更新单元格。

我做错了什么?

- (void)reloadTableView:(UITableView *)tableView
{
    NSArray *itemsArray = self.childItems;

    NSMutableArray *insertPath = [NSMutableArray array];
    NSMutableArray *deletePath = [NSMutableArray array];

    [itemsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (![itemsArray containsObject:obj]) {
            [insertPath addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
        }
    }];

    [itemsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([itemsArray containsObject:obj]) {
            [deletePath addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
        }
    }];

    if ([insertPath count] || [deletePath count]) {
        [tableView beginUpdates];

        if ([deletePath count]) {
            [tableView reloadRowsAtIndexPaths:deletePath withRowAnimation:UITableViewRowAnimationFade];
        }

        if ([insertPath count]) {
            [tableView reloadRowsAtIndexPaths:insertPath withRowAnimation:UITableViewRowAnimationFade];
        }
        [tableView endUpdates];
    }
    else {
        [tableView reloadData];
    }
}

1 个答案:

答案 0 :(得分:1)

您只是重新加载要删除或插入的行。而是删除要删除的行并插入要插入的行。

[tableView beginUpdates];

if ([deletePath count]) {
    [tableView deleteRowsAtIndexPaths:deletePath withRowAnimation:UITableViewRowAnimationFade];
}

if ([insertPath count]) {
    [tableView insertRowsAtIndexPaths:insertPath withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];