I'm having a problem that is driving me nuts for a while and I can not explain what is going wrong. I have a UITableView with different views and 3 prototype cells to select from. If I'm in my group mode I show the prototype applicationCell form my storyboard. First entry always looks ok, but if i have 2 the image in second entry doubles and resizes the image so that is shown twice - in correct size and again resized to cell height (also there exists only one image in per cell), see image:
Here the relevant code that causes the problem:
- (UITableViewCell *)tableView:(UITableView *)table
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(app == nil && groupedMode) {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"applicationCell"
forIndexPath:indexPath];
UILabel *head = (UILabel*)[cell viewWithTag:2];
UILabel *badge = (UILabel*)[cell viewWithTag:3];
UILabel *lastMsg = (UILabel*)[cell viewWithTag:4];
UIImageView *imgView = (UIImageView*)[cell viewWithTag:1];
ApplicationEntity *a = [applications objectAtIndex:indexPath.row];
head.text = a.name;
badge.text = [NSString stringWithFormat:@"%lu",(unsigned long)a.messages.count];
ImagesEntity *ie = [ImagesEntity imageByAppId:a.appToken imgId:0];
NSLog(@"V %li",(long)indexPath.row);
//imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.image = ie.computedImage;
CGRect r = imgView.bounds;
NSLog(@"bounds %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
r = imgView.frame;
NSLog(@"frame %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
imgView.bounds = CGRectMake(0,0,48,48);
imgView.frame = CGRectMake(11,2,48,48);
cell.tag = indexPath.row;
if(cell.gestureRecognizers.count == 0) {
UITapGestureRecognizer * gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(applicationClicked:)];
[cell addGestureRecognizer:gr];
}
lastMsg.text = [dateFormatter stringFromDate:a.lastMessage];
r = imgView.bounds;
NSLog(@"after bounds %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
r = imgView.frame;
NSLog(@"after frame %f %f %f %f",r.origin.x,r.origin.y,r.size.width,r.size.height);
return cell;
}
The UIImageView has a constraint for width = height = 48. What I tested and found out so far:
- Setting image to nil shows no image = no error
- Not assigning a new image does not show the error.
- Assigning a new image only once works.
When it happens the UIImageView gets new bounds and frame, but setting them directly back to correct values does not help see log
V 0 bounds 0.000000 0.000000 48.000000 48.000000 frame 11.000000 2.000000 48.000000 48.000000 after bounds 0.000000 0.000000 48.000000 48.000000 after frame 11.000000 2.000000 48.000000 48.000000 V 1 bounds 0.000000 0.000000 320.000000 110.000000 frame 0.000000 110.000000 320.000000 110.000000 after bounds 0.000000 0.000000 48.000000 48.000000 after frame 11.000000 2.000000 48.000000 48.000000
The 110 height is the cell height and 320 the width. It violates the constraints. Moreover the image I assign is 96x96 pixel so it shows nice in HDPI displays.
The ordering and images may change so I need to assign the right images.
Any ideas why this happens and how I could prevent this?