UITableViewCell重复图像

时间:2013-09-05 07:54:35

标签: iphone uitableview

我正在尝试在表格视图单元格中添加收藏夹图片。我正在使用此代码

static NSString *CellIdentifier = @"memberCell";
MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *item = self.searchList[indexPath.row];

cell.memberFullName.text = [item[@"full_name"] uppercaseString] ;
cell.memberPosition.text =  [item[@"title"] length] < 1 ? [NSString stringWithFormat:@"%@",  item[@"districts"]] : [NSString stringWithFormat:@"%@, %@", item[@"title"], item[@"districts"]];
cell.memberRoom.text = [NSString stringWithFormat:@"Rm %@", item[@"room_number"] ];
[cell.memberImage setImageWithURL:[NSURL URLWithString:item[@"image_thumb"]]
                 placeholderImage:[UIImage imageNamed:@"placeholder"]];

if(![item[@"fid"] isEqualToString:@"0"]) {
    [cell.memberFavoriteImage setImage:[UIImage imageNamed:@"favorite"]];
}

我遇到的问题是,当我执行if(![item[@"fid"] isEqualToString:@"0"])时,即使条件NSLog仅针对 1个单元格传递,多个单元格上显示最喜欢的图像,条件只传递一次,但显示的图像是一个系统的顺序(即每个第9行),当我一直向下滚动并返回时,顺序完全改变,图像显示在不同的行中。我不确定发生了什么,请帮忙。

2 个答案:

答案 0 :(得分:5)

dequeue指令可能会返回现有的单元格实例

试试这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
    MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *item = self.searchList[indexPath.row];
    cell.memberFullName.text = [item[@"full_name"] uppercaseString] ;
    cell.memberPosition.text =  [item[@"title"] length] < 1 ? [NSString stringWithFormat:@"%@",  item[@"districts"]] : [NSString stringWithFormat:@"%@, %@", item[@"title"], item[@"districts"]];
    cell.memberRoom.text = [NSString stringWithFormat:@"Rm %@", item[@"room_number"] ];
    [cell.memberImage setImageWithURL:[NSURL URLWithString:item[@"image_thumb"]]placeholderImage:[UIImage imageNamed:@"placeholder"]];

    if(![item[@"fid"] isEqualToString:@"0"]) {
        [cell.memberFavoriteImage setImage:[UIImage imageNamed:@"favorite"]];
    } else {
        [cell.memberFavoriteImage setImage:nil];
    }

    return cell;
}

答案 1 :(得分:3)

NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
    MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *item = self.searchList[indexPath.row];
    cell.memberFullName.text = [item[@"full_name"] uppercaseString] ;
    cell.memberPosition.text =  [item[@"title"] length] < 1 ? [NSString stringWithFormat:@"%@",  item[@"districts"]] : [NSString stringWithFormat:@"%@, %@", item[@"title"], item[@"districts"]];
    cell.memberRoom.text = [NSString stringWithFormat:@"Rm %@", item[@"room_number"] ];
    [cell.memberImage setImageWithURL:[NSURL URLWithString:item[@"image_thumb"]]placeholderImage:[UIImage imageNamed:@"placeholder"]];
    if(![item[@"fid"] isEqualToString:@"0"]) {
        [cell.memberFavoriteImage setImage:[UIImage imageNamed:@"favorite"]];
    }

    return cell;