views:

829

answers:

4

Hey everyone,

UITableview should only load cells that is visible at first right? My tableview is loading every cell initially which slows it down a lot. I'm using around 1000 rows. Only want it to load a cell when it has to (like user scrolling down). Anyone have any ideas why it's doing this?

A: 

Can you please post some code? The method -tableView:cellForRowAtIndexPath is only called when a new "slot" for a potential cell opens up, so you'd need to be doing something majorly wrong to be "loading every cell" initially!

Do you perhaps mean that you're loading all the data initially, and wish to do that in batches?

h4xxr
A: 

That's right - it should only load the ones it has to. What is your UITableView's rowHeight? If that was extremely low, the Table might expect to have to load all the cells

If that's not the problem, can you paste in your tableView:cellForRowAtIndexPath: code?

Rob Fonseca-Ensor
even at 1 pixel per row (!) he'd still not manage to load half his cells in that method. Surely this question must be about time to load data?
h4xxr
AFAIK, the only way to make tableView:cellForRowAtIndexPath: get called 1000 times when the view is first loaded is to set the rowheight to 0.
Rob Fonseca-Ensor
A: 

I know cellForRowAtIndexPath is getting called initially for every cell. The height of the cells is 89.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UILabel *textName    = nil;
UIImageView* image    = nil;
unsigned int  DATA_TAG = 1001;
unsigned int  IMG_TAG  = 1002;

// Retrieve a cell is Available
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Check if no new cell was available
if (cell == nil) 
{
 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

 // Set the Accessory Data
 textName = [[[UILabel alloc]initWithFrame:CGRectMake(cell.frame.origin.x, 80, cell.frame.size.width, 20)]autorelease];
 textName.tag     = DATA_TAG;
 textName.textAlignment   = UITextAlignmentCenter;
 textName.backgroundColor  = [UIColor clearColor];
 textName.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
 textName.textColor    = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
 textName.lineBreakMode   = UILineBreakModeWordWrap;

 [cell.contentView addSubview:textName];

 //Set the Image Data
 image = [[[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 80)]autorelease];
 image.tag = IMG_TAG;
 image.contentMode= UIViewContentModeScaleAspectFit;
 [cell.contentView addSubview:image];

}

Accessory* acc= [[AccessoryManager sharedManager].currentList objectAtIndex:indexPath.row];
if(acc == nil)
 return cell;


textName= (UILabel*)[cell.contentView viewWithTag:DATA_TAG];
textName.text= acc.accessoryName;


image= (UIImageView*)[cell.contentView viewWithTag:IMG_TAG];
[image setImage:acc.accessoryImage]; 
return cell;
}
If you're adding more info you should edit the original question rather than posting an "answer" to your own question...
h4xxr
Where is cell declared, and why are you calling [aTableView dequeue.. instead of [tableView dequeue...
Rob Fonseca-Ensor
Echoing @Rob's concerns. Also, please post your `- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath` for reference.
Jason
I found out calling [pTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animDirection] is reloading all the cells(all of them are in one section). How would I animate only the ones that will be visible.
That shouldn't happen. See the comment above about posting your -heightForRowAtIndexPath.Also, you may want to edit your question to include the code in this "answer". We usually look there to answer a question, not in comments for an answer.
mahboudz
A: 

Are you invoking cellForRowAtIndexPath yourself, for example from heightForRowAtIndexPath? If so, you don't need to create the cell to determine its height.

Amagrammer