0

I recently updated to the latest version of Xcode and now i am witnessing some weird behaviour with cell heights in my table. Using iOS7.0 in the simulator and devices was fine but after updating to iOS7.1 the issue is occurring. The issue was NOT occurring on devices with iOS7.1. There has been no code changes after the update.

Essentially, the cells render with correct height initially. The cell will completely disappear from the view when scrolling down, but when it re-appears after scrolling back up, the height has changed by about 20px. The real weird part is when scrolling DOWN again, near the bottom of the view, the cell will change height to the correct assigned height. It will literally jump 20px (approximately) while scrolling.

The code for the cell height is as follows. There is no other code within the view controller that accesses/edit the cells height.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

     CGRect frame = cell.frame;

     return frame.size.height;

}

Has anyone else experienced this, i have googled pretty hard on this but have come up empty handed. I really need to solve this as my client needs the app uploaded ASAP. hopefully it all makes sense.

EDIT 1: Code

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

NSString *simpleTableIdentifier;
NSLog(@"index %d", indexPath.row);


if (indexPath.row == (rowCount -1)){

    simpleTableIdentifier = @"moreCellAuto";

}else{

    simpleTableIdentifier = @"homeCell";
    homeReel = [searchResults objectAtIndex:indexPath.row];
    //NSLog(@"crash");
}





UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];



}

if([simpleTableIdentifier isEqualToString:@"homeCell"]){

    [vidIndex addObject:[homeReel objectForKey:@"VID"]];
    [refIndex addObject:[homeReel objectForKey:@"ref"]];
    [likeIndex addObject:[homeReel objectForKey:@"likes"]];
    [userIndex addObject:[homeReel objectForKey:@"UID"]];
    [urlIndex addObject:[homeReel objectForKey:@"URL"]];
    [siteIndex addObject:[homeReel objectForKey:@"site"]];
    if((NSNull *)[homeReel objectForKey:@"thumb"] != [NSNull null]){
        [thumbIndex addObject:[homeReel objectForKey:@"thumb"]];

    }else {

        [thumbIndex addObject:@""];
    }

    NSString* title = [homeReel objectForKey:@"title"];
    NSString* URL = [homeReel objectForKey:@"ref"];
    NSString* postdate = [homeReel objectForKey:@"postdate"];
    NSString* userName = [homeReel objectForKey:@"username"];
    NSString* likes =[homeReel objectForKey:@"likes"];
    NSString* commentsCount =[homeReel objectForKey:@"comment_count"];
    NSString* userSub = [homeReel objectForKey:@"userSub"];
    NSString* reposts = [homeReel objectForKey:@"reposts"];
    NSString* isFeat = [homeReel objectForKey:@"is_featured"];
    NSString* type = [homeReel objectForKey:@"type"];
    NSString* watchType = [homeReel objectForKey:@"watchType"];

    NSString* imageName = [homeReel objectForKey:@"image"];



    UILabel *postedBy = (UILabel *)[cell viewWithTag:165];

    NSString *postedByText;

    if([type isEqualToString:@"featured"]){

        postedByText = @"Vindl featured video";

    } else {

        if([displayType isEqualToString:@"latest"]){

            postedByText = [NSString stringWithFormat:@"Posted by %@  Watched by %@", userName, userSub];

        } else if ([displayType isEqualToString:@"friend"]){

            if (watchType != [NSNull null]){

                if([watchType isEqualToString:@"l"]){

                    postedByText = [NSString stringWithFormat:@"Posted by %@  Liked by %@", userName, userSub];

                }else if([watchType isEqualToString:@"c"]){


                    postedByText = [NSString stringWithFormat:@"Posted by %@  Commented on by %@", userName, userSub];
                }

            }

        } else {

            postedByText = [NSString stringWithFormat:@"Posted by %@", userName];

        }

    }

    postedBy.text = postedByText;
    postedBy.layer.shadowColor = [[UIColor blackColor] CGColor];
    postedBy.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    postedBy.layer.shadowOpacity = 1.0f;
    postedBy.layer.shadowRadius = 1.0f;


    UILabel *labelPosted = (UILabel *)[cell viewWithTag:101];
    labelPosted.layer.shadowColor = [[UIColor blackColor] CGColor];
    labelPosted.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    labelPosted.layer.shadowOpacity =1.0f;
    labelPosted.layer.shadowRadius = 1.0f;
    labelPosted.text = [adVC makeDate:postdate];


    UILabel *labelTitle = (UILabel *)[cell viewWithTag:100];
    labelTitle.text = title;
    labelTitle.layer.shadowColor = [[UIColor blackColor] CGColor];
    labelTitle.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    labelTitle.layer.shadowOpacity =1.0f;
    labelTitle.layer.shadowRadius = 1.0f;


    UILabel *labelLikes = (UILabel *)[cell viewWithTag:103];


    if([likes isEqualToString:@"1"]){

        labelLikes.text = [NSString stringWithFormat:@"%@ Like", likes];

    }else{

        labelLikes.text = [NSString stringWithFormat:@"%@ Likes", likes];

    }

    labelLikes.layer.shadowColor = [[UIColor blackColor] CGColor];
    labelLikes.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    labelLikes.layer.shadowOpacity =1.0f;
    labelLikes.layer.shadowRadius = 1.0f;


    UILabel *labelComCount = (UILabel *)[cell viewWithTag:104];

    if([commentsCount isEqualToString:@"1"]){

        labelComCount.text = [NSString stringWithFormat:@"%@ Comment", commentsCount];

    }else{

        labelComCount.text = [NSString stringWithFormat:@"%@ Comments", commentsCount];

    }

    labelComCount.layer.shadowColor = [[UIColor blackColor] CGColor];
    labelComCount.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    labelComCount.layer.shadowOpacity =1.0f;
    labelComCount.layer.shadowRadius = 1.0f;


    UILabel *labelReposts = (UILabel *)[cell viewWithTag:106];

    if([reposts isEqualToString:@"1"]){

        labelReposts.text = [NSString stringWithFormat:@"%@ Repost", reposts];

    }else{

        labelReposts.text = [NSString stringWithFormat:@"%@ Reposts", reposts];

    }

    labelReposts.layer.shadowColor = [[UIColor blackColor] CGColor];
    labelReposts.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    labelReposts.layer.shadowOpacity =1.0f;
    labelReposts.layer.shadowRadius = 1.0f;

    UIButton *buttonCom = (UIButton *)[cell viewWithTag:111];
    [buttonCom addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];


    //VID PREVIEW IMAGES

    NSString *identifierV = URL;

    UIImageView *cellImg = (UIImageView *)[cell viewWithTag:144];

    if ([cachedImagesVid objectForKey:identifierV] != nil){

        cellImg.image = [cachedImagesVid valueForKey:identifierV];

    } else {

        char const * s = [identifierV  UTF8String];

        dispatch_queue_t queue = dispatch_queue_create(s, 0);

        dispatch_async(queue, ^{

            NSString *urlVid = [thumbIndex objectAtIndex:indexPath.row];



            UIImage *imgVid = nil;

            NSData *dataVid = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlVid]];

            imgVid = [[UIImage alloc] initWithData:dataVid];

            dispatch_async(dispatch_get_main_queue(), ^{


                [cachedImagesVid setValue:imgVid forKey:identifierV];

                cellImg.image = [cachedImagesVid valueForKey:identifierV];


            });
        });
    }



    //USER IMAGES

    UIImageView *profileImage = (UIImageView *)[cell viewWithTag:112];

    if([type isEqualToString:@"featured"]){

        profileImage.image = [UIImage imageNamed:@"Featured-icon.png"];

    } else {

        NSString *identifier = userName;



        if ([cachedImages objectForKey:identifier] != nil){

            profileImage.image = [cachedImages valueForKey:identifier];

        } else {

            char const * s = [identifier  UTF8String];

            dispatch_queue_t queue = dispatch_queue_create(s, 0);

            dispatch_async(queue, ^{

                NSString *url = [NSString stringWithFormat:@"http://vindl.com/profiles/%@", imageName];

                UIImage *img = nil;

                NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

                img = [[UIImage alloc] initWithData:data];

                dispatch_async(dispatch_get_main_queue(), ^{



                        [cachedImages setValue:img forKey:identifier];

                        profileImage.image = [cachedImages valueForKey:identifier];


                });
            });
        }

    }



    UIView *bView1 = (UIView *)[cell viewWithTag:130];
    bView1.layer.cornerRadius = 3;
    bView1.layer.masksToBounds = YES;

    UIView *bView2 = (UIView *)[cell viewWithTag:131];
    bView2.layer.cornerRadius = 3;
    bView2.layer.masksToBounds = YES;

    UIView *bView3 = (UIView *)[cell viewWithTag:132];
    bView3.layer.cornerRadius = 3;
    bView3.layer.masksToBounds = YES;

    UIView *bView4 = (UIView *)[cell viewWithTag:133];
    bView4.layer.cornerRadius = 3;
    bView4.layer.masksToBounds = YES;



}


[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

[actInd stopAnimating];


return cell;


}
  • can you paste your code in cellForRowAtIndex ? – thndrkiss Aug 13 '14 at 02:47
  • 1
    As thndrkiss is alluding to, you are likely having a problem where the cells are being reused (or more specifically, not reused properly) in your cellForRowAtIndex method. The behavior you are seeing is common when iOS tries, for memory efficiency, to reuse cells that have scrolled off screen, rather than allocating and initializing new cells for all cells as they appear on screen. So it is probably keeping the height from the old cell, which would cause what you are seeing. – wottle Aug 13 '14 at 03:35
  • check the size of the device(3.5/4) while running the simulator? or in the storyboard? – Saheb Roy Aug 13 '14 at 04:59
  • is the UITableViewCell has dynamic content? if yes can you put the code of it – Janani M Aug 13 '14 at 05:06
  • Are you changing frames of any views in `cellForRowAtIndexPath` ? – GoodSp33d Aug 13 '14 at 07:00
  • I have posted the cellForAtRowIndex. in answer to the above questions: 1. there should be no height change for the cells from the cellForRowIndex, even when re-using cells as they are all the same height. 2. happens on a 3.5/4" device AND in the simulator 3. frame heights are not changed/edited in cellForRowIndex. thanks everyone for helping! – Michael Pierotti Aug 14 '14 at 03:12
  • Take a look at this: [http://stackoverflow.com/questions/22358831/ios-7-1-uitableviewcell-content-overlaps-with-ones-below](http://stackoverflow.com/questions/22358831/ios-7-1-uitableviewcell-content-overlaps-with-ones-below) – Wayn Liu Aug 14 '14 at 03:19
  • Brilliant. The clipToBounds did it. I didnt have this issue prior though. Weird. Thanks everyone for your help! – Michael Pierotti Aug 14 '14 at 06:40

1 Answers1

0

For anyone facing similar issues in iOS8, remember you don't need to implement heightForRowAtIndexPath. iOS 8 has made this pretty easy, while configuring your cell just set

numberOfLines = 0 if you are using the available cell styles.

For custom cells use auto layout and setTranslatesAutoresizingMaskIntoConstraints(false) along with numberOfLines = 0 if you have a UILabel.

runios
  • 384
  • 1
  • 2
  • 14