自定义TableViewCells不被重用

时间:2015-09-08 21:45:08

标签: ios uitableview tableviewcell

我的聊天应用程序使用以下cellForRowAtIndexPath为用户设置聊天对话框历史记录:

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

{

QBChatMessage *message = [[ChatService shared] messagsForDialogId:self.dialog.ID][indexPath.row];

    if (message.attachments.count > 0) {

        ImageTableViewCell *cell = [[ImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier message:message];

    [cell fillWithStickerMessage:message];
    cell.backgroundColor = [UIColor whiteColor];

    }

return cell;

}

以下是init文件中的ImageTableViewCell.m方法。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier message:(QBChatMessage *)message  {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        NSData *imageData = [FTWCache objectForKey:[NSString stringWithFormat:@"%@", [message.attachments[0] valueForKey:@"ID"]]];

        if (imageData) {

            UIImage *image = [UIImage imageWithData:imageData];
            [self.cellImage setImage:image];

        } else {

            for(QBChatAttachment *attachment in message.attachments){
                // download file by ID

                [QBRequest TDownloadFileWithBlobID:[attachment.ID integerValue] successBlock:^(QBResponse *response, NSData *fileData) {

                    [FTWCache setObject:fileData forKey:[NSString stringWithFormat:@"%@", attachment.ID]];

                    UIImage *image = [UIImage imageWithData:imageData];
                    [self.cellImage setImage:image];

                } statusBlock:^(QBRequest *request, QBRequestStatus *status) {
                    // handle progress
                } errorBlock:^(QBResponse *response) {
                    NSLog(@"error: %@", response.error);
                }];
            }
        }

        self.nameAndDateLabel = [[UILabel alloc] init];
        self.timeLabel = [[UILabel alloc] init];
        self.cellImage = [[UIImageView alloc] init];

        self.cellImage.opaque = YES;

        if ((IS_IPHONE_4) || (IS_IPHONE_5)){
            [self.nameAndDateLabel setFrame:CGRectMake(20, 5, 300, 20)];
        } else if (IS_IPHONE_6) {
            [self.nameAndDateLabel setFrame:CGRectMake(20, 5, 355, 20)];
        } else if (IS_IPHONE_6_PLUS) {
            [self.nameAndDateLabel setFrame:CGRectMake(20, 5, 394, 20)];
        }

        [self.nameAndDateLabel setFont:[UIFont boldSystemFontOfSize:15]];
        [self.nameAndDateLabel setTextColor:[UIColor lightGrayColor]];
        [self.contentView addSubview:self.nameAndDateLabel];

        self.backgroundImageView = [[UIImageView alloc] init];
        [self.backgroundImageView setFrame:CGRectZero];
        [self.backgroundImageView addSubview:self.cellImage];
        [self.contentView addSubview:self.backgroundImageView];

    }

    return self;

}

问题是当滚动表视图时,单元格会在initWithStyle文件的ImageViewCell.m中不断初始化。难道只有在细胞尚未创建的情况下才会发生这种情况吗?我做错了什么?

1 个答案:

答案 0 :(得分:1)

你错过了在cellForRowAtIndexPath中的调用

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
                           forIndexPath:(NSIndexPath *)indexPath

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

取决于您是否注册了UITableViewCell笔尖或类。

例如,在cellForRowAtIndexPath中你可以这样做:

ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];
if (cell == nil) {
    cell = [[ImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier message:message];
}