2012-11-08 119 views
0

我有一個錯誤。 我想在特殊indexPath.row的單元上顯示UIImageView,但這些UIImageView在我滾動時重複。 例:我顯示在一個單元格我UIImageViewindexPath.row == 0,如果我向下滾動,我看到了我的細胞上UIImageViewindexPath.row == 8UITableViewCell中的UIImageView重複

這裏是我的代碼:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil) { 
      cell = [self getCellContentView:CellIdentifier]; 

     } 

     UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1]; 
     UIImageView *imgRead = (UIImageView *)[cell viewWithTag:6]; 

     [cell.contentView insertSubview:imgRead aboveSubview:lblTemp1]; 

     contentDictio = [dict objectAtIndex:indexPath.row]; 

     lblTemp1.text = [contentDictio objectForKey:@"title"]; 

     NSArray *paths_id = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *basePath_id = ([paths_id count] > 0) ? [paths_id objectAtIndex:0] : nil; 
     NSString *path_id = [basePath_id stringByAppendingPathComponent:@"id.plist"]; 

     NSMutableArray *mut_array_id = [[NSArray arrayWithContentsOfFile:path_id] mutableCopy]; 

     NSMutableDictionary *c0 = [[NSMutableDictionary alloc] init]; 

     NSString *idPlistData = [contentDictio objectForKey:@"id"]; 

     for(c0 in mut_array_id) { 
      if([[c0 objectForKey:@"id"] isEqualToString:idPlistData]) { 
       [imgRead setImage:[UIImage imageNamed:@"read"]]; 
      } 
      else { 
      } 
     } 
    } 

    return cell; 
} 

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier { 

    CGRect Label1Frame = CGRectMake(kTableCellSmallMargin*2 + 60, kTableCellSmallMargin, 240, 25); 

    UILabel *lblTemp; 

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                reuseIdentifier:cellIdentifier] autorelease]; 

    //Initialize Label with tag 1. 
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame]; 
    lblTemp.tag = 1; 
    lblTemp.backgroundColor = [UIColor clearColor]; 
    [lblTemp setFont: [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:15.0]]; 
    [cell.contentView addSubview:lblTemp]; 
    [lblTemp release]; 

    UIImageView *read=[[UIImageView alloc] initWithFrame:CGRectMake(kTableCellSmallMargin, kTableCellSmallMargin, 60, 60)]; 
    read.backgroundColor=[UIColor clearColor]; 
    read.tag = 6; 
    [cell.contentView addSubview:read]; 
    [read release]; 

    return cell; 
} 

謝謝...

回答

0

細胞被緩存,所以你必須要清除它,當你不想要的圖像,像這樣:

BOOL found = NO; 
    for(c0 in mut_array_id) { 
     if([[c0 objectForKey:@"id"] isEqualToString:idPlistData]) { 
      [imgRead setImage:[UIImage imageNamed:@"read"]]; 
      found = YES; 
     } 
     else { 

     } 
    } 

    if (!found) 
    { 
     [imgRead setImage:nil]; 
    } 
相關問題