Ivan Kozlov
Ivan Kozlov

Reputation: 561

iOS table view low perfomance

I'm really in deadlock. I've got a tableview with uilabels and images. Images downloads with HJcache library. All works fine, but very very slow, here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *currentCellId = @"currentCell";
NSUInteger currentRow = [indexPath row];
HJManagedImageV* currentImage;
UILabel *textLabel;
UIImageView *onlineStatusView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentCellId];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:currentCellId];
    currentImage = [[HJManagedImageV alloc] initWithFrame:CGRectMake(2,2,40,40)];
    [currentImage setTag:999];
    [cell addSubview:currentImage];
    textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 2.0f, cell.frame.size.width - 50.0f, cell.frame.size.height-4.0f)];
    [textLabel setTag:777];
    [textLabel setFont:[UIFont boldSystemFontOfSize:12.0f]];
    [cell addSubview:textLabel];
    if ([[[dataArray objectAtIndex:currentRow] objectForKey:@"online"] integerValue] == 1){
        onlineStatusView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.size.width-15.0f,
                                                                         cell.frame.size.height/2,
                                                                         10.0f,
                                                                         10.0f)];
        [onlineStatusView setTag:888];
        [cell addSubview:onlineStatusView];
    }
} else{
    onlineStatusView = (UIImageView *)[cell viewWithTag:888];
    textLabel = (UILabel *)[cell viewWithTag:777];
    currentImage = (HJManagedImageV*)[cell viewWithTag:999];
    [currentImage clear];
}

currentImage.url = [NSURL URLWithString:[[dataArray objectAtIndex:currentRow] objectForKey:@"image"]];
[cache manage:currentImage];
[currentImage setOpaque:YES];
currentImage = nil;

NSString *nameString = [NSString stringWithFormat:@"%@ %@",
                  [[dataArray objectAtIndex:currentRow] objectForKey:@"firstName"],
                  [[dataArray objectAtIndex:currentRow] objectForKey:@"lastName"]];
[textLabel setText:nameString];
[textLabel setOpaque:YES];
textLabel = nil;

if (onlineStatusView){
    [onlineStatusView setImage:[UIImage imageNamed:@"[email protected]"]];
    [onlineStatusView setOpaque:YES];
    onlineStatusView = nil;
}

return cell;

}

All looks pretty good, I removed all unnecessary allocations, cached images, but all without avail. What I'm doing wrong?

ps now i've got around 200 rows, but bugs starts when it was 20. I guess my mistake really stupid.

Upvotes: 0

Views: 325

Answers (2)

ott--
ott--

Reputation: 5722

Insert some NSLog() around your cache:manage calls to see how long they really last. Don't worry about your 200 rows, cellForRowAtIndexPath is only called for the visible rows.

Upvotes: 0

jrturton
jrturton

Reputation: 119272

Couple of issues here:

  • you're setting your currentImage to opaque before it exists, so this has no effect. Do it after you create and add the image to your cell as a subview
  • you're creating new labels all the time. These should only be added once, like your currentImage, and just have the text modified
  • same goes for the online status view. Create it once when you make the cell, and just make it hidden or not depending on your model.

I don't know about the library you are using so can't comment on that.

Upvotes: 1

Related Questions