我在 TableView 中使用自定义单元格。
单元格高度是根据加载到单元格的 UILabel 中的 NSString 计算的。使用此函数计算大小
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [self getTableViewRow:tableView index:indexPath];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2) + 60;
}
大小计算正确,但是当单元格加载到 uiTableView 时,行具有正确的高度,但单元格没有。
这是我创建我的单元格的地方
//Com Custom Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellApresentacao" owner:self options:nil];
if ([nib count] > 0 )
cell = self.tvCell;
else
NSLog(@"Falhou a carregar o xib");
}
NSInteger row = [indexPath row];
if(self.myTableView1 == tableView)
labelDescricaoApresentacao.text = [listData1 objectAtIndex:row];
else if (self.myTableView2 == tableView)
labelDescricaoApresentacao.text = [listData2 objectAtIndex:row];
else
labelDescricaoApresentacao.text = [listData3 objectAtIndex:row];
return cell;
}
我尝试使用这种方法更改单元格高度
cell.frame.size.height
但它仍然没有加载正确的高度。
我必须在 customCell 的 xib 中做任何事情吗?我应该如何将单元格高度更新为与行相同的大小?