UITableView:加载所有单元格

问题描述 投票:3回答:4

是否可以在加载视图时加载UITableView的所有单元格,以便在滚动时不加载它们?(我将在执行此操作时显示加载屏幕)

请,这是我项目中的唯一方法(对不起,很难解释为什么^^)

编辑:

好,让我向您解释,我肯定在做:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *cellIdentifier = [NSString stringWithFormat:@"Identifier %i/%i", indexPath.row, indexPath.section];
   CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   NSDictionary *currentReading;

   if (cell == nil)
   {
       cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

       UILabel *label;
       UIView *separator;

       if(indexPath.row == 0)
       {
           // Here I'm creating the title bar of my "table" for each section
       }
       else
       {
           int iPr = 1;

           do
           {
               currentReading = [listData objectAtIndex:iPr-1];
               iPr++;
           } while (![[currentReading valueForKey:@"DeviceNo"] isEqualToString:[devicesArr objectAtIndex:indexPath.section]] || 
                      [readingresultsArr containsObject:[currentReading valueForKey:@"ReadingResultId"]]);

           [readingresultsArr addObject:[currentReading valueForKey:@"ReadingResultId"]];
           //
           // ...
           //
       }
    }
    return cell;
}

我的错误发生在do-while循环中:“ listData”是其中包含多个字典的数组。我的问题是,当我缓慢向下滚动表时,一切都很好,但是当我快速滚动到视图的末尾然后又滚动到中间时,我得到了iPr不在其中的错误数组的范围。因此,问题在于,第一节的最后一行已被添加到“ readingresultsArr”中,但尚未加载或希望再次加载。这就是为什么我要一次加载所有单元的原因。

objective-c xcode uitableview viewdidload
4个回答
11
投票

您可以通过调用以下命令来预先分配所有单元格:

[self tableView: self.tableView cellForRowAtIndexPath: indexPath];

对于表中的每一行。将以上行放入适当的for循环中,然后在viewDidAppear中执行此代码。

但是问题是tableView不会保留所有这些单元格。当不需要它们时,它将丢弃它们。

您可以通过在NSMutableArray中添加UIViewController来解决该问题,然后缓存在cellForRowAtIndexPath中创建的所有单元格。如果在表的生命周期内对表进行了动态更新(插入/删除),则还必须更新缓存阵列。


8
投票

将uitableview放在uiscrollview上

例如,您期望整个列表uitableview的高度为1000

然后设置uiscrollview内容大小为320X1000并将uitableview的高度设置为1000

然后所有单元格加载其内容,即使在屏幕上也不可见


3
投票

在我的情况下,我使用了automaticDimension作为单元格的高度,并将估计的RowHeight设置为small,这就是为什么tableview加载所有单元格的原因>


0
投票

[根据juancazalla的评论,我发现如果您有一个使用autoDimension的tableView,则可以通过将估计的行高度设置为较低的值(例如1)来最好地一次加载所有单元格。

© www.soinside.com 2019 - 2024. All rights reserved.