SDWebImage和UITableViewCell

时间:2011-06-10 12:55:22

标签: xcode cocoa-touch uitableview sdwebimage

我正在使用SDWebImage和UITableView

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

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier"];
    NSDictionary *info = [tableData objectAtIndex:indexPath.row];

    UITableViewCell *cell = [the_tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
        if(!addImage) [addImage release];
        addImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder.png"]];
        [addImage setFrame:CGRectMake(7, 10, 50, 50)];
        [cell setIndentationLevel:6];
        [cell.contentView addSubview:addImage];
    }

    if(info !=  NULL) {
        cell.textLabel.text = [info objectForKey:@"title"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Version: %@",[info objectForKey:@"version"]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        NSString *imageURL = [NSString stringWithFormat:@"%@",[info objectForKey:@"imageURL"]];
        [addImage setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];         
    }
    return cell;
}

哪个适用于前6个结果(适用于即时视图的数量)

但是当我向下滚动列表时,似乎它只是重新使用前6个单元格中的图像,而在某些单元格中,图像会根据它们在屏幕上的位置而改变。

另外,如果我调用reloadData,之前UITableCells的图像会保留在屏幕上!

我在这里做错了吗?我已经按照github上的示例代码进行了..

谢谢!

1 个答案:

答案 0 :(得分:0)

(OP在问题编辑中回答。在这里作为社区维基回答。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

好的,所以我发现了问题。

     

对于那些有同样问题的人来说,这就是你做错了!

     

注意我在创建单元格时如何将图像添加到SubView中:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
addImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder.png"]];
[addImage setFrame:CGRectMake(7, 10, 50, 50)];
[cell.contentView addSubview:addImage];
  

嗯,SDWebImage尝试做的是不断更新addImage变量,这不是我的单元格类的属性。

     

所以,我为解决这个问题所做的是创建我自己的UITableViewCell子类,使用ImageView初始化,然后我SDWebImagesetImage在那个属性上!

     

我希望这有助于某人!