0

我正在使用自定义单元格来显示图像。但是,当我将图像添加到第一行时,它会每第四行加载一次。可能是什么问题?我正在使用 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;
}
4

1 回答 1

1

iPhone SDK通过重用不在视图中的单元而不是一直重新创建新单元来UITableView优化创建。UITableViewCell这是通过您调用dequeueReusableCellWithIdentifier:.

tableView:cellForRowAtIndexPath:中,您需要确保您出列的单元格被清除,以便它只包含新的单元格内容。在这种情况下,有一个cell.imageView.image = nilbefore 你的if (r == 1) ...声明就足够了。

于 2009-12-01T12:20:30.453 回答