dimanche 1 mars 2015

UITableView Cell showing wrong image

I have a IUTableView 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)
{
knife.knifeThumbnail = [[NSString alloc] initWithString:[knife.knifeImageNames 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]];


}


Aucun commentaire:

Enregistrer un commentaire