SDWebImage 和 UITableViewCell

发布于 2024-11-15 00:02:14 字数 1585 浏览 3 评论 0原文

我正在使用 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,之前的 UITableCell 中的图像将保留在屏幕上!

我在这里做错了什么吗?我已经按照 github 上的示例代码进行了操作。

谢谢!

I'm working with SDWebImage and 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;
}

Which Works Great for the first 6 Results (The amount that can fit on the immediate view)

But as I scroll down the list, it seems like it's just re-using images from the first 6 cells, and on some cells images change depending on their location on the screen.

Also, if I call reloadData, the images from the previous UITableCells stay on screen!

Am I doing something wrong here? I've followed the example code on github..

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2024-11-22 00:02:14

(OP 在问题编辑中回答。作为社区 wiki 答案移至此处。请参阅 问题没有答案,但问题在评论中解决(或在聊天中扩展)

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 初始化,并且我得到 SDWebImage在该属性上setImage

我希望这对某人有帮助!

(Answered by the OP in a question edit. Moved here as a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

OK, So I found the problem.

For anyone else out there with the Same issue, this is where you're doing it wrong!

Notice how I'm adding the Image into SubView while creating a cell:

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];

Well, what SDWebImage was trying to do is constantly update that addImage variable, which wasn't a property of my cell class.

So, what I did to fix this problem is create my own UITableViewCell subclass, that init's with a ImageView and I get SDWebImage to setImage on that property!

I hope this helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文