[英]How do I add a UITextView to this UITableViewCell?
I have a UITableViewCell
that is dynamically sized based on the content in it. 我有一个
UITableViewCell
,它根据其中的内容动态调整大小。 I do this in heightForRowAtIndexPath
as follows: 我在
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;
}
}
In cellForRowAtIndexPath
I customise the cell as follows: 在
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;
}
This all works fine, however I want the content of the cell to detect phone numbers, dates/times etc. So I think I need to have the content in a UITextView
within the UITableViewCell
to make this possible. 一切正常,但是我希望单元格的内容能够检测电话号码,日期/时间等。因此,我认为我需要将内容包含在
UITableViewCell
的UITextView
中,以实现此目的。
How can I dynamically size the UITextView
and add it to the cell with the content by modifying my code above? 如何通过修改上面的代码动态调整
UITextView
大小并将其添加到具有内容的单元格中? Sample code please with answers because I've already tried adding the UITextView as a subview of the cells contentView but it's not working as I'd expect. 请提供示例代码并给出答案,因为我已经尝试过将UITextView添加为单元格contentView的子视图,但是它没有按我期望的那样工作。
您可以使用sizeToFit
或sizeThatFits:
方法来调整UITextView
对象的大小
While creating cell, you need to add textView as its subView and no need to set textView size separately. 创建单元格时,您需要添加textView作为其子视图,而无需单独设置textView的大小。 You just need add margin values while returning height for row in
您只需要在返回行的高度时添加边距值
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.