开发者

SDWebImage and UITableViewCell

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, 5开发者_Go百科0)];
        [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!


(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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜