2009-12-01 75 views
0

我正在使用自定義的單元格來顯示圖像。但是,當我將圖像添加到第一行時,它會被加載到第四行。可能是什麼問題?我正在使用uiimagepickercontroller從iPhone照片庫中選取圖像,並將其提供給表格的第一個單元格。uitableviewcell問題

- (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; 
} 
+0

我也面臨這個問題......你是怎麼解決它的? – 2013-07-28 19:10:01

回答

1

的iPhone SDK UITableView通過重用不在視圖,而不是重新創建新的所有時間單元優化UITableViewCell創作。這是通過您撥打dequeueReusableCellWithIdentifier:完成的。

tableView:cellForRowAtIndexPath:中,您需要確保您出列的單元格被清除,因此它只包含新的單元格內容。在這種情況下,在您的if (r == 1) ...聲明之前有cell.imageView.image = nil就足夠了。