【发布时间】:2013-02-20 16:50:41
【问题描述】:
我有一个UITableViewCell,它根据其中的内容动态调整大小。我在heightForRowAtIndexPath 中这样做如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) {
NSDictionary *fields = self.messageDetailsDictionary[@"fields"];
NSString *cellText = fields[@"message_detail"];
UIFont *cellFont = [UIFont systemFontOfSize:14.0];
CGSize constraintSize = CGSizeMake(self.tableView.frame.size.width - 50.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20.0f;
} else {
return tableView.rowHeight;
}
}
在cellForRowAtIndexPath我自定义单元格如下:
case 1:{
UITableViewCell *cell = [[UITableViewCell new] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"];
if (cell == nil) {
cell = [[UITableViewCell new] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"];
}
NSDictionary *fields = self.messageDetailsDictionary[@"fields"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.numberOfLines = 0; // This means multiline
cell.textLabel.text = fields[@"message_detail"];
return cell;
}
这一切都很好,但是我希望单元格的内容能够检测电话号码、日期/时间等。所以我认为我需要在UITableViewCell 中的UITextView 中包含内容才能实现这一点。
如何通过修改上面的代码来动态调整UITextView 的大小并将其添加到包含内容的单元格中?示例代码请提供答案,因为我已经尝试将 UITextView 添加为单元格 contentView 的子视图,但它没有像我预期的那样工作。
【问题讨论】:
标签: ios uitableview uitextview