0

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:@"Online@2x.png"]];
    [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.

Ivan Kozlov
  • 561
  • 2
  • 8
  • 19
  • Best answered here: http://stackoverflow.com/questions/1352479/tricks-for-improving-iphone-uitableview-scrolling-performance – Oded Ben Dov Jun 13 '12 at 12:06

2 Answers2

1

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thank you. I made some changes, but on device i again get memory warning. Please check my new code. – Ivan Kozlov Mar 11 '12 at 18:05
  • I thought your problem was slow scrolling, not memory? If memory, it may be to do with the HJCache library, I don't know if you are using that right as I've never used it. – jrturton Mar 11 '12 at 18:39
  • Ok, i removed all images, but all stayed the same. Tableview with only labels have a slow scrolling problem. – Ivan Kozlov Mar 11 '12 at 18:57
  • As i commented below this function on initializing calls 1900 times. – Ivan Kozlov Mar 11 '12 at 19:00
  • Are you calling it manually from anywhere? What's in your numberOfSections/numberOfRows methods? – jrturton Mar 12 '12 at 08:23
0

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.

ott--
  • 5,642
  • 4
  • 24
  • 27
  • Yes i know about visible cells. I've just checked that on start this function calls 1900 times! I think it's not very good, isn't it? – Ivan Kozlov Mar 11 '12 at 18:38