Tableview单元格高度变化
Posted
技术标签:
【中文标题】Tableview单元格高度变化【英文标题】:Tableview cell height changing 【发布时间】:2014-08-13 02:38:29 【问题描述】:我最近更新到最新版本的 Xcode,现在我看到我的表格中的单元格高度出现了一些奇怪的行为。在模拟器和设备中使用 ios7.0 很好,但在更新到 iOS7.1 后问题出现了。使用 iOS7.1 的设备上未出现此问题。更新后代码没有变化。
基本上,单元格最初以正确的高度呈现。向下滚动时,单元格将完全从视图中消失,但在向上滚动后重新出现时,高度已经改变了大约 20px。真正奇怪的部分是当再次向下滚动时,在视图底部附近,单元格将高度更改为正确的分配高度。它会在滚动时跳跃 20 像素(大约)。
单元格高度的代码如下。视图控制器中没有其他代码可以访问/编辑单元格高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGRect frame = cell.frame;
return frame.size.height;
有没有其他人经历过这种情况,我在谷歌上搜索了很多,但空手而归。我真的需要解决这个问题,因为我的客户需要尽快上传应用程序。希望这一切都有意义。
编辑 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;
【问题讨论】:
你能把你的代码粘贴到 cellForRowAtIndex 吗? 正如 thndrkiss 所暗示的,您可能在 cellForRowAtIndex 方法中重复使用(或更具体地说,未正确重复使用)单元格的问题。当 iOS 尝试重用已滚动到屏幕外的单元格时,您看到的行为很常见,而不是在所有单元格出现在屏幕上时为其分配和初始化新单元格。所以它可能会保持与旧单元格的高度,这会导致您所看到的。 在运行模拟器时检查设备的大小(3.5/4)?还是在故事板中? UITableViewCell 有动态内容吗?如果是,你能把它的代码放上去吗 您是否正在更改cellForRowAtIndexPath
中任何视图的框架?
【参考方案1】:
对于在 iOS8 中遇到类似问题的任何人,请记住您不需要实现 heightForRowAtIndexPath。 iOS 8 让这一切变得非常简单,只需设置您的单元格即可
numberOfLines = 0 如果您使用可用的单元格样式。
对于自定义单元格,使用自动布局和 setTranslatesAutoresizingMaskIntoConstraints(false) 如果您有 UILabel,则与 numberOfLines = 0 一起。
【讨论】:
以上是关于Tableview单元格高度变化的主要内容,如果未能解决你的问题,请参考以下文章
表格视图高度之外的行的单元格内容发生变化(要查看此单元格,我们向下滚动)?