简体   繁体   中英

UITableView Cell showing wrong image

I have a UITableView with two Cells. One Cell with an image and one without an image. When the Cell without the image is shown below the Cell with the image everything is fine ( Image 1 ). But when I sort the list and the Cell without the image is shown above the Cell with the image, the Cell without the image is showing the same image as the other Cell ( Image 2 ). I am struggling with this problem for days now but I can't find out where the problem is.

Here is the Code for setting the image:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

Object *myObject = [[Datamodel sharedInstance] objectAtIndex:(int)indexPath.row];
cell.textLabel.text = myObject.name;

if([myObject.imageNames count] != 0 && !myObject.thumbnail)
{
    myObject.thumbnail = [[NSString alloc] initWithString:[myObject.imageNames objectAtIndex:0]];
}

if(myObject.thumbnail)
{
    NSLog(@"Setting thumbnail");
    NSString *pathToImgData= [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ]stringByAppendingPathComponent:@"ImgData"];
    NSMutableString *imgPath = [[NSMutableString alloc] initWithString:pathToImgData];
    [imgPath appendString:@"/"];
    [imgPath appendString: myObject.thumbnail];
    UIImage* image = [UIImage imageWithContentsOfFile:imgPath];
    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(8, 8, 128, cellHeight-16)];
    imv.image = image;
    [cell addSubview:imv];
}
return cell;
}

Regarding the console output only one object enters the if(myObject.thumbnail) condition so only for the Cell that should have an image, the image ist set:

2015-03-01 ***** Cell with image *****
2015-03-01 --> Setting the image

2015-03-01 ***** Cell wihout image *****
2015-03-01 --> NOT setting the image

2015-03-01 ***** Cell without image *****
2015-03-01 --> NOT setting the image
2015-03-01 ***** Cell with image *****
2015-03-01 --> Setting the image

And the Code for sorting the list:

-(void)sortList{

NSSortDescriptor *sortObjectName = [NSSortDescriptor sortDescriptorWithKey:@"objectName" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1 caseInsensitiveCompare:obj2];
}];
[self.arrayData sortUsingDescriptors:@[sortObjectName]];
}

Your problem is that you don't set the image of the cell that you don't want to nil .

The UITableViewCells are reused, so you need to configure every value in the cell that you want or don't want.

Just figure out if you need to present the image, if so set it, if not then set the

UIImageView.image = nil;

You have to remove the image, when you have no thumbnail, since the cells are reused:

if(myObject.thumbnail)
{
    NSLog(@"Setting thumbnail");
    NSString *pathToImgData= [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ]stringByAppendingPathComponent:@"ImgData"];
    NSMutableString *imgPath = [[NSMutableString alloc] initWithString:pathToImgData];
    [imgPath appendString:@"/"];
    [imgPath appendString: myObject.thumbnail];
    UIImage* image = [UIImage imageWithContentsOfFile:imgPath];
    UIImageView *imv = [cell viewWithTag:1234];
    if (!imv) {
        imv = [[UIImageView alloc]initWithFrame:CGRectMake(8, 8, 128, cellHeight-16)];
        [cell addSubview:imv];
        imv.tag = 1234;
    }
    imv.image = image;
} else {
    UIView *imageView = [cell viewWithTag:1234];
    if (imageView) {
        [imageView removeFromSuperview];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM