2014-05-12 37 views
0

每當我刪除單元格,這是以前在其位imageViews不消失的單元格。 例如一個小區使用所有的三個圖像視圖,和被刪除,通過一個使用的圖像視圖之一的細胞所取代。這兩個圖像的視圖與前一個單元的圖像(已刪除)保留。 當我刪除一個細胞,我從一個NSMutableArray刪除對象,做reloadData上的UITableView。自定義UITableViewCells重裝問題

我做了一個自定義的UITableViewCell,它看起來像

@interface MyPetTableViewCell : UITableViewCell 

@property (nonatomic, weak) IBOutlet PetNameLabel *petName; 
@property (nonatomic, weak) IBOutlet UIImageView *imageOne; 
@property (nonatomic, weak) IBOutlet UIImageView *imageTwo; 
@property (nonatomic, weak) IBOutlet UIImageView *imageThree; 

@end 

我的UITableView是這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"PetCell"; 
    MyPetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    PetResponse *petItem = self.petListResponse.PetList[indexPath.row]; 
    NSString *petNameItem = petItem.PetName; 
    NSLog(@"PET NAME : %@",petNameItem); 
    NSLog(@"NUMBER OF IMAGES : %lu", (unsigned long)petItem.EncodedImages.count); 
    for (int i = 0; i < [petItem.EncodedImages count]; i++) { 

     NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:petItem.EncodedImages[i] options:0]; 
     UIImage *image = [UIImage imageWithData:decodedData]; 
     if (i == 0) { 
      cell.imageOne.image = image; 
     } else if (i == 1) 
     { 
      cell.imageTwo.image = image; 
     } else if (i == 2) 
     { 
      cell.imageThree.image = image; 
     } 
    } 
    cell.petName.text = [NSString stringWithFormat:@"%@ :",petNameItem]; 
    UIColor *customcolor = [UIColor colorWithRed:11/255.0f green:64/255.0f blue:64/255.0f alpha:0.5f]; 
    cell.petName.backgroundColor = customcolor; 
    cell.petName.layer.cornerRadius = 5; 
    cell.petName.numberOfLines = 0; 
    [cell.petName sizeToFit]; 

    [cell setBackgroundColor:[UIColor clearColor]]; 

    NSLog(@"Returning Cell"); 
    return cell; 
} 
+0

顯示also..how要刪除對象 –

回答

0

這是很難猜測發生了什麼設置,但一種可能性是:
表視圖單元格被重新使用。所以,當你添加,例如,一個子視圖到表格視圖單元格,表視圖的相應行滾動出屏幕,電池(與子視圖!)放回再使用游泳池。如果應該顯示一個新的表格行,那麼現在未使用的單元格(包含舊的子視圖)將從重用池中獲取,並顯示舊的子視圖。
你可以在這種情況下,做的是「再利用準備一個表格視圖單元格」,使用方法

- (void)prepareForReuse 

在這種方法中,你可以刪除所有舊子視圖,看到docs

+0

由於工作完全 –

+0

從你的鏈接你刪除單元代碼前將所有圖像nil,它說'prepareForReuse'應該只用於與內容無關的單元屬性。在這種情況下,應該使用tableView:cellForRowAtIndexPath,因爲他正在更改圖像內容。 –

0

聽起來與細胞再利用的問題。作爲蘋果的文件說:

表視圖的中的tableView委託:的cellForRowAtIndexPath:重用小區時應該 始終重置所有內容。

一個可能的解決辦法是賦予新的圖像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     static NSString *CellIdentifier = @"PetCell"; 
     MyPetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     PetResponse *petItem = self.petListResponse.PetList[indexPath.row]; 
     NSString *petNameItem = petItem.PetName; 
     NSLog(@"PET NAME : %@",petNameItem); 
     NSLog(@"NUMBER OF IMAGES : %lu", (unsigned long)petItem.EncodedImages.count); 

     cell.imageOne.image = nil; 
     cell.imageTwo.image = nil; 
     cell.imageThree.image = nil; 

     for (int i = 0; i < [petItem.EncodedImages count]; i++) { 

      NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:petItem.EncodedImages[i] options:0]; 
      UIImage *image = [UIImage imageWithData:decodedData]; 
      if (i == 0) { 
       cell.imageOne.image = image; 
      } else if (i == 1) 
      { 
       cell.imageTwo.image = image; 
      } else if (i == 2) 
      { 
       cell.imageThree.image = image; 
      } 
     } 
     cell.petName.text = [NSString stringWithFormat:@"%@ :",petNameItem]; 
     UIColor *customcolor = [UIColor colorWithRed:11/255.0f green:64/255.0f blue:64/255.0f alpha:0.5f]; 
     cell.petName.backgroundColor = customcolor; 
     cell.petName.layer.cornerRadius = 5; 
     cell.petName.numberOfLines = 0; 
     [cell.petName sizeToFit]; 

     [cell setBackgroundColor:[UIColor clearColor]]; 

     NSLog(@"Returning Cell"); 
     return cell; 
    } 
相關問題