简体   繁体   English

UITableView单元格显示错误的图像

[英]UITableView Cell showing wrong image

I have a UITableView with two Cells. 我有一个带有两个单元格的UITableView 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 ). 当没有图像的单元格显示在具有图像的单元格下面时,一切都很好( 图像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 ). 但是,当我对列表进行排序时,没有图像的单元格显示在带有图像的单元格上方,则没有图像的单元格显示的图像与另一个单元格相同( 图像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: 关于控制台输出,只有一个对象进入if(myObject.thumbnail)条件,因此仅对于应该具有图像的单元格设置图像:

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 . 您的问题是,您不会设置您不想设为nil的单元格的图像。

The UITableViewCells are reused, so you need to configure every value in the cell that you want or don't want. UITableViewCells被重用,因此您需要在单元格中配置所需或不需要的每个值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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