2013-02-20 54 views
0

我有一个UITableViewCell根据它的内容动态调整大小。我这样做是heightForRowAtIndexPath如下:如何添加一个UITextView到这个UITableViewCell?

- (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的子视图,但它不像我期望的那样工作。

回答

0

在创建细胞,你需要的TextView添加作为其子视图,并没有必要单独设置TextView的大小。您只需添加边距值,同时返回行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
相关问题