2

我有显示酒店的表格视图,以及他们是否喜欢。如果宿舍是最喜欢的,在宿舍行附件视图中我放置了最喜欢的图像。但图像不仅仅出现在目标的宿舍。我随机放置在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;
}

我正在搜索和搜索,有什么问题?

4

3 回答 3

3

由于 UITableView 正在重用单元格,因此您的设计存在一些问题。

您将 UIImageView 添加到单元格,即使它被重用,所以例如当您有 50 行并且您一直向下滚动时,每次重用一个单元格时,您都在向单元格添加一个 UIImageView。所以刷新内存。

最好将 UITableViewCell 子类化,在其中添加 UIImageView。不要忘记覆盖 prepareForReuse 方法,您可以在其中将 UIImageView 的图像设置为 nil

于 2012-07-21T20:51:31.980 回答
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.
}
于 2012-07-21T20:43:08.180 回答
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;

}
于 2012-07-21T23:29:50.567 回答