开发者

UITableView cell image problem

I am tr开发者_如何学运维ying to import an image in just cell number 1 and 2 ! , but I the result is my image will show in last cell ! I do not know why !! this is the picture that shows my situation :

UITableView cell image problem

    // Configure the cell.
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [titles objectAtIndex:row];
    cell.detailTextLabel.text = [subtitle objectAtIndex:row]; 


    switch (indexPath.row) {
        case 0:
            cell.imageView.image = [UIImage imageNamed:@"new.png"];
            break;

        case 1:
            cell.imageView.image = [UIImage imageNamed:@"new.png"];
            break;
    }



    return cell;

}


Either in your tableView:cellForRowAtIndexPath: set the imageView's image to nil before conditionally checking to set the new image or implement prepareForReuse on your cell subclass and set all of the cell's views values to nil.

This will ensure that reused cells are 'clean' before they're brought on screen.

Alternatively you could edit your switch to look like:

switch (indexPath.row) {
    case 0:
        cell.imageView.image = [UIImage imageNamed:@"new.png"];
        break;

    case 1:
        cell.imageView.image = [UIImage imageNamed:@"new.png"];
        break;
    default:
        cell.imageView.image = nil;
        break;
}


The first thing that it makes me think of is that the first cell gets somehow recycled and used for the last cell. You can try to set the image view to nil for every cell and set it just in the first two cells. Should be something like this:

cell.imageView.image = nil;

Hope it helps! ;D


You might have used "static NSString *reuseIdentifier = @"Identifier" ", this means that all cells will be considered same by this reuseIdentifier. Only visible cells would be different so for example, if there are 5 visible cells on device, then only 5 new cells would be allocated for cell, and then if you scroll down or up, these 5 cells will be reused if you specified reuseIdentifier statically.

I would suggest to make cell uniquely identified by reuseIdentifier, change the above line to "NSString *reuseIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row]" This would solve the issue.

Hope this helps.


Your cells are reused by the tableview to save memory. You have to tell the cell not to use an image on every other cell. Modify the switch command like this:

 switch (indexPath.row) {
    case 0:
        cell.imageView.image = [UIImage imageNamed:@"new.png"];
        break;

    case 1:
        cell.imageView.image = [UIImage imageNamed:@"new.png"];
        break;

    default:
        cell.imageView.image = nil;
        break;
}

Sandro Meier


try this it might help you

    case 0:
        cell.imageView.image = [UIImage imageNamed:@"new.png"];
        break;

    case 1:
        cell.imageView.image = [UIImage imageNamed:@"new.png"];
        break;
     default
        cell.imageView.image=nil;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜