UITableView: load all cells

一曲冷凌霜 提交于 2019-12-23 18:21:18

问题


Is it possible to load all cells of an UITableView when the view is loaded so that they are not loaded when I'm scrolling? (I would show a loading screen while doing this)

Please, it's the only way at my project (sorry too complicate to explain why ^^)

EDIT:

Okay let me explain you, what I'm definite doing:

- (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;
}

My error happens in the do-while-loop: "listData" is an array with multiple dictionaries in it. My problem ist that when I’m scrolling my table slowly down, all is fine, but when I’m scrolling quickly to the end of the view and then I’m scrolling to the middle, I get the error that iPr is out of the array’s range. So the problem is, that the last row of the first section has already been added to the "readingresultsArr", but has not been loaded or wants to be loaded again. That’s the reason why I want to load all cells at once.


回答1:


You can cause all of the cells to be pre-allocated simply by calling:

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

for every row in your table. Put the above line in an appropriate for-loop and execute this code in viewDidAppear.

The problem however is that the tableView will not retain all of these cells. It will discard them when they are not needed.

You can get around that problem by adding an NSMutableArray to your UIViewController and then cache all the cells as they are created in cellForRowAtIndexPath. If there are dynamic updates (insertions/deletions) to your table over its lifetime, you will have to update the cache array as well.




回答2:


put a uitableview on a uiscrollview

for example , you expect the height of the full list uitableview is 1000

then set the uiscrollview contentsize is 320X1000 and set the uitableview height is 1000

then all cell load their content even not visible in screen




回答3:


in my case it was that I used automaticDimension for cells height and put estimatedRowHeight to small that is why tableview loaded all cells



来源:https://stackoverflow.com/questions/12268812/uitableview-load-all-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!