我有桌面视图,显示酒店以及他们是否喜欢。如果宿舍是最喜欢的,在旅馆的行配件视图中放置了喜欢的图像。但是这张照片并没有只显示在目标的宿舍。我随机放在tableView中的其他单元格中。这是代码:
- (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;
}
我正在搜索和搜索,问题是什么?
答案 0 :(得分:3)
由于UITableView正在重复使用单元格,因此您的设计存在一些问题。
你将UIImageView添加到单元格中,即使它被重用,所以当你有50行并且你一直向下滚动时,每次重复使用一个单元格时,你都会向单元格添加一个UIImageView。所以冲刷记忆。
最好是在UITableViewCell的子类中添加UIImageView。不要忘记覆盖prepareForReuse方法,您可以将UIImageView的图像设置为nil
答案 1 :(得分: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.
}
答案 2 :(得分: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;
}