0

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

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"]) 仅通过1 个单元格,当我执行时NSLog,条件仅通过一次,但显示的图像是系统顺序(即每 9 个行),当我一直向下滚动并返回时,顺序完全改变,图像显示在不同的行中。我不确定发生了什么,请帮忙。

4

2 回答 2

5

dequeue 指令可能会返回一个现有的 cell 实例

尝试这个

- (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;
}
于 2013-09-05T08:28:14.653 回答
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;
于 2013-09-05T08:08:04.403 回答