自定义UITableViewCell和慢速滚动

时间:2012-02-16 23:00:27

标签: ios uitableview scroll

我需要两种不同高度的UITableViewCell:

a)一个带有小UIImageView的用户图片,3个UILabel和一个200 x 100的UIImageView。

b)另一个具有相同的a)没有UIImageView。

好吧,为了实现这一点,我已经构建了两个自定义UITableViewCell并设置我的UITableView来使用它们。

问题是当我滚动表格时,因为有时它不顺畅。

我的代码如下(我只发布了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;
    }

}

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:3)

您的代码可能存在的问题是您的Cell从未被重用过。 NSBundle loadNibNamed:每次表需要时创建新的Cell。因此,如果dataSource包含10000条记录 - 您将创建10000个单元格。

请检查您是否在xib文件中设置了单元格标识符。这将导致您的细胞出列。