uitableviewcell问题

时间:2009-12-01 11:59:39

标签: iphone uitableview uiimagepickercontroller photo

我正在使用自定义单元格来显示图像。但是,当我向第一行添加图像时,每隔第4行加载一次。可能是什么问题?我正在使用uiimagepickercontroller从iphone照片库中选择图像并将其提供给表格的第一个单元格。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    CGSize newSize = CGSizeMake(80, 80);
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageView.image = newImage;

    [picker dismissModalViewControllerAnimated:YES];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
        for (id currentObject in nib){
            if ([currentObject isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *)currentObject;
                //[cell loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
                break;
            }
        }
    }

    NSUInteger r= [indexPath row];
    NSUInteger s= [indexPath section];
    //[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];
    // NSInteger r= indexPath.row;
    if(r == 1)
        if (s== 0){
             UIImage *img=imageView.image;
             cell.imageView.image = img;
        }
     return cell;
}

1 个答案:

答案 0 :(得分:1)

iPhone SDK UITableView通过重复使用不在视图中的单元格来优化UITableViewCell创建,而不是一直重新创建新单元格。这是通过致电dequeueReusableCellWithIdentifier:完成的。

tableView:cellForRowAtIndexPath:中,您需要确保您出列的单元格被清除,因此它只包含新的单元格内容。在这种情况下,在cell.imageView.image = nil语句之前加if (r == 1) ...即可。