奇怪的行为dynamicuitableviewcell高度

我试图使我的custome细胞dynamicuitableviewcell高度。

单元格被添加一些背景的子类。

这是我的uitableview控制器类:

#define PADDING 23.0f - (void)viewDidLoad { [super viewDidLoad]; [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSUInteger count = [self.entries count]; return count + _rowcount; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuseIdentifier = @"PlaceholderCell2"; SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (sctvCell == nil) { sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; } NSUInteger nodeCount = [self.entries count]; sctvCell.contentView.translatesAutoresizingMaskIntoConstraints = NO; UILabel *label = (UILabel *)[sctvCell.contentView viewWithTag:1]; if (nodeCount > 0) { NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row]; [label setText:appRecord.title]; NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"B MyFont" size:14.0f]}; CGRect rect = [appRecord.title boundingRectWithSize:CGSizeMake(label.frame.size.height - PADDING * 5, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; CGRect newFrame = label.frame; newFrame.size.height = rect.size.height; label.frame = newFrame; [label sizeToFit]; UIView *whiteRoundedCornerView = (UIView *)[sctvCell.contentView viewWithTag:1000]; CGRect newFrame2 = whiteRoundedCornerView.frame; newFrame2.size.width = 300; newFrame2.size.height = rect.size.height + 160; [ whiteRoundedCornerView setFrame:newFrame2]; } if ((unsigned long)indexPath.row == [self.entries count] - 1){ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ NewsFetchParseOperation *p = [[NewsFetchParseOperation alloc]init]; NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row]; p.cat = appRecord.Category; self.intindex = self.intindex + 1; p.index = [NSString stringWithFormat:@"%d", (int)self.intindex]; p.lastid = appRecord.ids; [p main]; dispatch_async(dispatch_get_main_queue(), ^(void) { [self.tableView beginUpdates]; NSMutableArray *indexPaths = [NSMutableArray array]; NSInteger currentCount = self.entries.count; for (NSUInteger i = 0; i < p.appRecordList.count; i++) { [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]]; } NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList]; self.entries = temp_1; [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; [self.tableView endUpdates]; }); }); } return sctvCell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuseIdentifier = @"PlaceholderCell2"; SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (sctvCell == nil) { sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; } NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row]; UILabel *label = (UILabel *)[sctvCell.contentView viewWithTag:1]; NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"B MyFont" size:14.0f]}; CGRect rect = [appRecord.title boundingRectWithSize:CGSizeMake(label.frame.size.height - PADDING * 5, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; return rect.size.height + PADDING * 6; } 

和我的细胞子类:

 - (void)awakeFromNib { self.news_img.layer.cornerRadius = 4; self.news_img.clipsToBounds = YES; self.resource_icon_img.layer.cornerRadius = 4; self.resource_icon_img.clipsToBounds = YES; self.contentView.backgroundColor = [UIColor clearColor]; self.whiteroundcorner = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,250)]; self.whiteroundcorner.backgroundColor = [UIColor whiteColor]; self.whiteroundcorner.layer.masksToBounds = NO; self.whiteroundcorner.layer.cornerRadius = 3.0; [self.whiteroundcorner.layer setShadowColor:[UIColor grayColor].CGColor]; self.whiteroundcorner.layer.shadowOffset = CGSizeMake(-1, 1); self.whiteroundcorner.layer.shadowOpacity = 0.2; self.whiteroundcorner.tag = 1000; [self.contentView addSubview:self.whiteroundcorner]; [self.contentView sendSubviewToBack:self.whiteroundcorner]; } 

即时通讯使用故事板为我的表像这样:

在这里输入图像说明

现在问题是大多数时候高度计算不正确。

也有的时候高度超出了细胞和10细胞的末端

当我尝试去取新行的最后一个单元格apears不正确。

您将需要在设置其内容之后计算单元格的高度。

所以像这样的东西:

  - (CGFloat)heightForCellAtIndexPath:(NSIndexPath *)indexPath { static UITableViewCell *sizingCell = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sizingCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; }); /* Method where you set the content of the cell */ [self configureCell: atIndexPath:indexPath]; return [self calculateHeightForConfiguredSizingCell:sizingCell]; } - (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell { [sizingCell setNeedsDisplay]; [sizingCell layoutIfNeeded]; CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; return size.height; }