2
votes

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:

enter image description here

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:

  1. Setting image to nil shows no image = no error
  2. Not assigning a new image does not show the error.
  3. Assigning a new image only once works.
  4. 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?

1
(Just an advice) Creating variables for UILabel and UIImageView is also good coding practice imo. It will make your code more readable and you will not have this kind of problems. And using custom cells is also make job more seperated and clean.meth

1 Answers

1
votes

After some googling through related answers I found at least a solution and an idea what happened.

Table cells have an own image already. And my code

UIImageView *imgView = (UIImageView*)[cell viewWithTag:1];

should have returned my UIImageView since it is the only one set with tag 1. Apparently it was not. Changing tag to 10 in storyboard and code solved it. So i was changing somehow the background image of the cell.

Strange thing is that the other mode has also prototype cells with the image as tag 1 and they do not have this kind of problem. So I still do not why it really happens, but at least I know how to fix it. Hope this is useful for others with similar problems.