How do I create a multiline table cell in iOS?

前端 未结 4 1531
暖寄归人
暖寄归人 2021-02-03 13:40

How can I get the second cell to expand to fit the text rather than scaling the text? Is there a built in way of doing this in iOS or will I have to come up with some home-cook

相关标签:
4条回答
  • 2021-02-03 13:49

    Where you have the address as the detailTextLabel, use a UILabel thats supports as many lines as you need, for an address, 3 should be enough. Together with Paul Bently's answer, you should be good to go.

    0 讨论(0)
  • 2021-02-03 13:58

    Unfortunately, you are going to have to implement this feature yourself. There are a variety of methods and callbacks you need to make use of to calculate the height of the rows and labels. If you need some help getting started, I can amend this answer to include some sample code. Otherwise, I'm sure there are some related questions here on SO or Google that can get you started. In summary, however:

    • Determine the height of the row in the UITableViewDelegate method -tableView:heightForRowAtIndexPath:. You'll probably need to use the NSString UIKit Addition methods to calculate how tall your string will be with a given font size, etc. Return the proper height from this delegate method.
    • Make sure your label (either in IB or configured programmatically) is set to use multiple lines. If you set numberOfLines to 0, the label will use as many lines as necessary.
    • In your UITableViewDataSource implementation of -tableView:cellForRowAtIndexPath:, you'll need to use the same logic as before to determine the frame of your labels. Set the text you want, then change the frame so that the label is tall enough to just barely fit all the text.
    0 讨论(0)
  • 2021-02-03 14:03

    Implement:

    - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    in your table delegate. For the different row and section in the passed indexPath, return a different height to fit your label.

    Ensure 'adjust to fit' for the label in IB is not ticked (property adjustsFontSizeToFitWidth if done using code).

    0 讨论(0)
  • 2021-02-03 14:12

    I just ran into this same problem. I found an alternative solution that doesn't require quite as much hardcoding, and allows you to make modifications to the label in Interface Builder that will be reflected in the height calculation.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         if (indexPath.section == 0 && indexPath.row == 1) {
            NSString *text = [atmAnnotation address];
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    
            cell.textLabel.text = text;
            [cell.textLabel sizeToFit];
    
            CGFloat height = MAX( 44, cell.textLabel.bounds.size.height );
    
            return height + CELL_CONTENT_MARGIN * 2;
        }
    
        return 44.0f;
    }
    

    The only downside is that this will cause some additional temporary UITableViewCell allocations, but the table view will immediately reclaim them, so I don't think it should be a big problem.

    0 讨论(0)
提交回复
热议问题