Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView: load all cells

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.

like image 866
Michael Avatar asked Nov 29 '22 16:11

Michael


1 Answers

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.

like image 178
Kent Avatar answered Dec 20 '22 14:12

Kent