由于某些奇怪的原因,TableView 单元格无法正确拟合动态文本内容。它们应该在单元格的顶部和底部之间留有少许边距。不幸的是,根据文本内容的数量,一些单元格没有顶部和底部边距(紧密包装),而另一些则有巨大的边距。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float result = 30;
if (indexPath.section != 0){
CGSize size = [[self textForIndexPath:indexPath isTitle:NO] sizeWithFont:kITDescriptionFont constrainedToSize:CGSizeMake(220, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
result = MAX(result, size.height);
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *doubleCellIdentifier = @"ITDoubleDetailCEllIdentifier";
static NSString *cellIdentifier = @"ITDetailCEllIdentifier";
NSString *cellIdent = (indexPath.section == 0)? doubleCellIdentifier : cellIdentifier;
ITDescriptionCell *cell = (ITDescriptionCell *)[tableView dequeueReusableCellWithIdentifier:cellIdent];
if (!cell) {
UITableViewCellStyle style = (indexPath.section == 0)? UITableViewCellStyleValue2 : UITableViewCellStyleDefault;
cell = [[ITDescriptionCell alloc] initWithStyle:style reuseIdentifier:cellIdent];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.textLabel setNumberOfLines:0];
[cell.textLabel setFont:kITDescriptionFont];
}
else {
[cell.textLabel setText:@""];
[cell.detailTextLabel setText:@""];
}
[cell sizeToFit];
return cell;
}