2012-07-21 64 views
2

我有表显示酒店,如果他们最喜欢或不。如果宿舍是最喜欢的,在旅馆的排行配件视图我放置了最喜欢的图像。但是这张图片并没有显示在目标旅舍。我随机放置在tableView中的其他单元格中。这里是代码:uitableview重复图像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 50) reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Set up the cell... 
     //First get the dictionary object 
     NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
     NSArray *array = [dictionary objectForKey:@"Countries"]; 
     NSString *cellValue = [array objectAtIndex:indexPath.row]; 
     cell.textLabel.text = cellValue; 

     if (![favorites count] == 0) { 
      if ([favorites containsObject:[array objectAtIndex:indexPath.row]]) { 
       NSUInteger indexx = [favorites indexOfObject:cellValue]; 
       if ([[favorites objectAtIndex:indexx] isEqual:cellValue]) { 

        // here is the image view 

        UIImageView *imageView = [[UIImageView alloc] init]; 
        [imageView setImage:[UIImage imageNamed:@"small_ItsMyFavorite.png"]]; 
        imageView.frame = CGRectMake(276, 0, 50, 50); 
        [cell addSubview:imageView]; 
        [imageView release]; 
       } 
      } 

     } 
    } 
} 

return cell; 
} 

我在寻找和搜索,是什么问题?

回答

2

由于UITableView重复使用单元,您的设计有一些问题。

即使将UIImageView添加到单元格中,即使它被重用,因此例如当您有50行并且一直向下滚动时,每次单元格被重用时,都会向单元格中添加UIImageView。冲洗记忆。

更好的是子类UITableViewCell中添加UIImageView。不要忘了覆盖prepareForReuse方法,其中,你会你的UIImageView的图像设置为nil

+0

工作非常好。它解决了问题! – theShay 2012-07-22 12:25:47

+0

太棒了!'prepareForReuse'帮我 – 2013-11-29 09:28:15

2

由于UITableViewCell对象被回收,您需要添加一个else分支来清除图像,当宿舍不是最喜欢的。正如你目前所拥有的,曾经用来显示喜欢的单元格将永远包含一个图像。

if ([[favorites objectAtIndex:indexx] isEqual:cellValue]) { 
    // here is the image view 
    UIImageView *imageView = [[UIImageView alloc] init]; 
    [imageView setImage:[UIImage imageNamed:@"small_ItsMyFavorite.png"]]; 
    imageView.frame = CGRectMake(276, 0, 50, 50); 
    [cell addSubview:imageView]; 
    [imageView release]; 
} else { 
    // Find the UIImageView in the subviews of your cell, 
    // and remove it from superview. 
} 
0

你必须做这样的事情

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 50) reuseIdentifier:CellIdentifier] autorelease]; 
} 
else 
{ 
    [[cell.contentView viewWithTag:999] removeFromSuperView]; 
} 

// Set up the cell... 
    //First get the dictionary object 
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Countries"]; 
    NSString *cellValue = [array objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    if (![favorites count] == 0) { 
     if ([favorites containsObject:[array objectAtIndex:indexPath.row]]) { 
      NSUInteger indexx = [favorites indexOfObject:cellValue]; 
      if ([[favorites objectAtIndex:indexx] isEqual:cellValue]) { 

       // here is the image view 

       UIImageView *imageView = [[UIImageView alloc] init]; 
       [imageView setImage:[UIImage imageNamed:@"small_ItsMyFavorite.png"]]; 
       imageView.frame = CGRectMake(276, 0, 50, 50); 
       imageView.tag = 999; // for example 
       [cell addSubview:imageView]; 
       [imageView release]; 
      } 
     } 

    } 
} 
} 

return cell; 

} 
+0

无法正常工作。我尝试过。 – theShay 2012-07-22 10:14:41