UITableViewCell的动态高度取决于文本

时间:2013-01-21 15:20:44

标签: iphone ios objective-c uitableview

我有一个带有正确细节单元布局的分组tableview。现在我从服务器上获取了数据。 有时我会收到一个非常大的文本。因此tableviewCell应该高于其他人。您可以在此处查看我的问题的屏幕截图。

enter image description here

就像你在我的第一个单元格中看到的那样,我有一个非常大的文本。现在我希望单元格的高度随着文本的长度而增长,所以我总能看到整个文本而不是'...'这就是我在代码中所拥有的。

#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];

    NSArray *meta = sub.meta.allObjects;

    NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];

    return textSize.height + PADDING * 3;
}

修改 我现在的代码。

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];

    NSArray *meta = sub.meta.allObjects;

    NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(330.0f, 400);  // Make changes in width as per your label requirement.

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return  textSize.height;
}

这就是结果。

enter image description here

编辑CellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];
    NSArray *meta = sub.meta.allObjects;
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


        cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_label"];
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.numberOfLines = 0;
        cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];

        cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
                                               green:102/255.0
                                                blue:102/255.0
                                               alpha:1.0];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        return cell;



}

2 个答案:

答案 0 :(得分:1)

您需要在代码中使用lineBreakMode:UILineBreakModeWordWrap

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);  // Make changes in width as per your label requirement. 

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return  textSize.height;
}

此外,您需要设置detailTextLabel also in datasource method cellForRowAtIndexPath`的两个属性,如下所示:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
   cell.detailTextLabel.numberOfLines = 0;
 }
  • numberofLines属性描述:

    此属性控制使用的最大行数,以便将标签的文本放入其边界矩形中。此属性的默认值为1.要删除任何最大限​​制,并根据需要使用尽可能多的行,请将此属性的值设置为0.

答案 1 :(得分:0)

试试这个:

 - (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
            Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];

            NSArray *meta = sub.meta.allObjects;

            NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
            UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];
            CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);  // Make changes in width as per your label requirement.

            CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return  textSize.height + 20; }

然后,考虑使用唯一的小区标识符。这里的想法是重复使用单元格将导致内容的高度不匹配。 (有些人不喜欢唯一标识符的概念,但它们有效,除非你的表格很大,否则它们不会有问题。)相应地,试试这个:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];
    NSArray *meta = sub.meta.allObjects;

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%i", indexPath.row]];

    if (cell == nil){
        cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_label"];
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.numberOfLines = 0;
        cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];

        cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
                                               green:102/255.0
                                                blue:102/255.0
                                               alpha:1.0];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
}
        return cell;
}