如何使UITableviewCell(自定义)高度取决于内容?

时间:2014-11-22 06:20:10

标签: ios objective-c uitableview uilabel

在我的UIViewController中,我有一个带自定义单元格的UITableView。

请查看下面的图片以获取截图。 enter image description here

因为我正在给予"多线"功能到UILabel(屏幕截图中选中的标签),但我现在有2个问题。

当我打开这个viewcontroller时,如果我的UILabel(描述标签)文本非常小,我可以看到很多空白区域。我该如何管理这些空格?

请查看应用截图 enter image description here

我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"cell";

    InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[InfoCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
    }

    cell.header.text=[names objectAtIndex:indexPath.row];
    cell.details.text=[details objectAtIndex:indexPath.row];
    [cell.header sizeToFit];
    [cell.details sizeToFit];
    return cell;
}

请帮助清除这些问题

  1. 如何使UILabel文字大小动态而非固定多线?

  2. 如何使UITableViewCell动态取决于其内容高度?

3 个答案:

答案 0 :(得分:1)

查看本教程:Ray Wenderlich Tutorial

它确切地处理了这个问题,并且会教你一些技巧;)

答案 1 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @“Your text”;
    UIFont *cellFont = [UIFont systemFontOfSize:YourFontSize];
    CGSize constraintSize = CGSizeMake(TextviewWidth, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize : %f", labelSize.height);
    return labelSize.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @“CustomCell”;

    CommentCell *cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSString *cellText = @“LabelText”;
    UIFont *cellFont = [UIFont systemFontOfSize:yourFontSize];
    CGSize constraintSize = CGSizeMake(LabelWidth, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

    cell.lbl.frame = CGRectMake(cell.lbl.frame.origin.x, cell.lbl.frame.origin.y, cell.lbl.frame.size.width, labelSize.height);
    cell.lbl.lineBreakMode = NSLineBreakByWordWrapping;
}

答案 2 :(得分:0)

这项工作适合我。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *strYourText = @"Testing Case ";
    NSMutableParagraphStyle *objPS = [[NSMutableParagraphStyle alloc] init];
    objPS.lineBreakMode=NSLineBreakByWordWrapping;//here you have to pass the Line break mode which you have to label
    CGSize labelSize = CGSizeMake(200.0, 1000.0);///width: label width and set the height 1000.0
    CGSize textSizeForLabel = [strYourText boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : objPS,NSFontAttributeName : [UIFont systemFontOfSize:18.0]} context:nil].size;
    float labelHeight =ceilf(textSizeForLabel.height);//this are the only lable height.if there are other UIControls then add their heights and retuns.
    //Like if One More Buttons is there in cell then return the button height with dynamic lable height (e.g. return labelHeight+buttonHeight );
//herer we are consider there is a only one label is there so return label height
    return labelHeight;




}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"cell";

    InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[InfoCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:MyIdentifier];
    }

    cell.header.numberOfLines = 0;
    cell.header.lineBreakMode=NSLineBreakByWordWrapping;

    NSString *strYourText = @"Testing Case ";
    NSMutableParagraphStyle *objPS = [[NSMutableParagraphStyle alloc] init];
    objPS.lineBreakMode=NSLineBreakByWordWrapping;//here you have to pass the Line break mode which you have to label
    CGSize labelSize = CGSizeMake(cell.header.frame.size.width, 1000.0);///width label width and set the height 1000.0
    CGSize textSizeForLabel = [strYourText boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : objPS,NSFontAttributeName : [UIFont systemFontOfSize:18.0]} context:nil].size;
    float labelHeight =ceilf(textSizeForLabel.height);
    [cell.header setFrame:CGRectMake(cell.header.frame.origin.x, cell.header.frame.origin.x, cell.header.frame.origin.x, labelHeight)];
    cell.header.text=strYourText;


    return cell;

}