【问题标题】:I'm trying to expand every UITableViewCell in a UITableView recursively by inserting the cells into the table, the cells aren't expanding correctly我试图通过将单元格插入表中递归地扩展 UITableView 中的每个 UITableViewCell,单元格没有正确扩展
【发布时间】:2014-06-14 06:54:42
【问题描述】:

表格从“折叠”的父单元格开始(代表这个的数组是第一次传递给函数的)。每个单元格在数组中由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];
        }
    }
}

【问题讨论】:

    标签: ios objective-c xcode uitableview nsmutablearray


    【解决方案1】:

    这个问题在错误中得到了准确的描述。您在迭代数组时正在对其进行变异。你说在第一次迭代中,你传入了 self.tableCells。所以 self.tableCells == cmets,并且在枚举 cmets 时,您将对象添加到 self.tableCells,从而对其进行变异。您不能将对象添加到您正在枚举的数组中。考虑将单元格存储在另一个数据结构中。

    【讨论】:

    • 我不确定你是否能看到我编辑的标题,但这不是我现在遇到的问题,我编辑了帖子以删除任何相关内容。现在只是结果不如预期。
    • 是的,你在我写回复和发布的时候编辑了它,在你改变它的 30 秒内完成了回复。发布您正在体验的照片和用于显示的代码,因为此代码示例是针对您的旧问题,并且仅与您的数据源操作有关,而不是 UI。
    • 这不太可能?对不起大佬,哈哈。好吧,显示它们的代码就是通常的 cellForRowAtIndexPath,其中的键“级别”决定缩进级别。由这个扩展函数来获取 cmets 数组并将它们插入到 cellForRowAtIndexPath 使用的数组中,并以正确的“级别”放置在正确的位置。
    • 这里是指向actual comments, 的链接(数据直接来自 Reddit,这里没有问题),这里是 cmets after being passed through this function。我无法理解它,只是问题在于它在同一级别上分配了具有递减“级别”值的 cmets。
    猜你喜欢
    • 2012-01-04
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    相关资源
    最近更新 更多