Gerardo
Gerardo

Reputation: 5830

Custom UITableViewCell and slow Scroll

i need two kinds of UITableViewCell that has different height:

a) One with a small UIImageView holding user picture, 3 UILabel's and one UIImageView of 200 x 100.

b) The other has the same like a) without the UIImageView.

Well, in order to achieve this i have built two custom UITableViewCell's and set my UITableView to use them.

The problem is when i scroll the table because sometimes it isn't smooth.

My code is the following (i only post the cellForRowAtIndexPath)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (.....) return HEIGHT1 
    else return HEIGHT2
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CheckinCellIdentifier";
    static NSString *TextCellIdentifier = @"CheckinTextCellIdentifier";
    CheckIn *checkin = [self.checkinArray objectAtIndex:indexPath.row];

    if (checkin.photoUrl) {

        CheckinUITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil){
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CheckinUITableViewCell" owner:nil options:nil];

            for(id currentObject in topLevelObjects) {
                if([currentObject isKindOfClass:[CheckinUITableViewCell class]]) {
                    cell = (CheckinUITableViewCell *)currentObject;
                    if (self.userImage)
                        cell.userImage.image = self.userImage;
                    break;
                }
            }
        }

        cell.checkin = checkin;    
        UIImage *imageCheckin = [self.checkinImages objectForKey:[NSNumber numberWithInt:checkin.checkInId]];
        if (imageCheckin) {
            cell.checkinImage.image = imageCheckin;
        } else {
            CompleteBlock block = ^(NSData* imageData) {
                if (imageData) {
                    UIImage *image = [UIImage imageWithData:imageData];
                    [self.checkinImages setObject:image forKey:[NSNumber numberWithInt:checkin.checkInId]];
                    CheckinUITableViewCell *lookedUpCell = (CheckinUITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
                    if (lookedUpCell){
                        cell.checkinImage.image = image;
                        //[lookedUpCell setNeedsLayout];
                    }
                }
            };
            [Utility loadImageFromFile:checkin.photoPath url:checkin.photoUrl withCompletionBlock:block];
        }
        return cell;
    } else {
        CheckinTextUITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
        if (cell == nil){
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CheckinTextUITableViewCell" owner:nil options:nil];

            for(id currentObject in topLevelObjects) {
                if([currentObject isKindOfClass:[CheckinTextUITableViewCell class]]) {
                    cell = (CheckinTextUITableViewCell *)currentObject;
                    if (self.userImage)
                        cell.userImage.image = self.userImage;
                    break;
                }
            }
        }
        cell.checkin = checkin;    
        return cell;
    }

}

Any idea? Thanks!

Upvotes: 0

Views: 1043

Answers (1)

SVGreg
SVGreg

Reputation: 2368

The possible problem of your code is that your Cells have never been reused. NSBundle loadNibNamed: create new Cell each time your table need it. So if dataSource contain 10000 records - you creates 10000 cells.

Please check if you set cell identifier within xib file. This will cause to dequeue your cells.

Upvotes: 3

Related Questions