Uitableviewcell高度变化

时间:2014-08-13 02:38:30

标签: ios objective-c

我最近更新到了Xcode的最新版本,现在我正在见证我桌子中细胞高度的一些奇怪行为。在模拟器和设备中使用iOS7.0很好,但在更新到iOS7.1之后,问题就出现了。 iOS7.1设备上没有出现此问题。更新后没有代码更改。

基本上,细胞最初以正确的高度渲染。向下滚动时,单元格将从视图中完全消失,但在向上滚动后重新出现时,高度已更改约20px。真正奇怪的部分是当再次向下滚动时,在视图底部附近,单元格将高度更改为正确的指定高度。滚动时,它会逐渐跳跃20px(大约)。

单元格高度的代码如下。视图控制器中没有其他代码可以访问/编辑单元格高度。

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

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

     CGRect frame = cell.frame;

     return frame.size.height;

}

有没有其他人经历过这一点,我在Google上搜索得非常努力,但空手而归。我真的需要解决这个问题,因为我的客户需要尽快上传应用程序。希望这一切都有道理。

编辑1:代码

-(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;


}

1 个答案:

答案 0 :(得分:0)

对于在iOS8中遇到类似问题的任何人,请记住您不需要实现heightForRowAtIndexPath。 iOS 8让这非常容易,而配置你的单元格只需设置

如果您使用可用的单元格样式,则numberOfLines = 0。

对于自定义单元格,使用自动布局和setTranslatesAutoresizingMaskIntoConstraints(false) 如果您有UILabel,则numberOfLines = 0。