我试图通过将单元格插入表格来递归扩展UITableView中的每个UITableViewCell,单元格无法正确扩展

时间:2014-06-14 06:54:42

标签: ios objective-c xcode uitableview nsmutablearray

该表以“折叠”父单元格开始(表示此数组的数组首先传递给函数)。每个单元格由数组NSDictionary表示。每个父单元格都有一个子单元格数组,每个子单元格都包含子单元格数组等。如果单元格没有子单元格,则字典中的“子”key将设置为空NSMutableArray }。

此函数的目标是使用NSMutableArray表示没有展开单元格的tableView,并递归扩展每个单元格及其子项(在Reddit上考虑注释线程),同时将展开的单元格添加到相应的原始数组。它工作但是,由于某种原因,单元格没有正确扩展。有些是扩展的,有些则不是,这很奇怪。有什么想法吗?

- (void)expandCells:(NSMutableArray *)comments
{
    for (NSDictionary *thread in comments)
    {
        // recursive loop using each child in the root's child array
        NSMutableArray *children = [thread objectForKey:@"children"];
        if (children && [children count] > 0)
        {
            [self expandCells:children];

            // indexPath of root of current thread
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[comments indexOfObjectIdenticalTo:thread] inSection:0];
            NSUInteger count = indexPath.row + 1;
            NSMutableArray *newCellIndexes = [NSMutableArray array];

            for (NSDictionary *child in children)
            {
                //[newCellIndexes addObject:[NSIndexPath indexPathForRow:count inSection:0]];

                // a copy of self.tableCells first passed to this function,
                // since you cannot modify an array while iterating over it
                [self.tableCells insertObject:child atIndex:count++];
            }

            //[self.tableView insertRowsAtIndexPaths:newCellIndexes withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题在错误中完全描述了。你在迭代它时改变了数组。你说在第一次迭代中,你传入self.tableCells。所以self.tableCells == comments,在枚举注释时,你将对象添加到self.tableCells,从而改变它。您无法将对象添加到您枚举的数组中。考虑将单元存储在另一个数据结构中。