How to resize UITableViewCell to fit any detail TextLabel

im creating an iphone application so that any text in detailTextlabel will match no matter what

for example ... if the detailTextLabel is short, it will show a normal size cell, however if it is long, it will display a large cell

Thank you: D

+3
source share
3 answers
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    CGSize maxSize = CGSizeMake(max_size);
    CGSize cellSize = [yourString 
                       sizeWithFont:[UIFont systemFontOfSize:15]
                       constrainedToSize:maxSize
                       lineBreakMode:UILineBreakModeWordWrap];
    return cellSize.height;

}
+2
source

Consider my implementation:

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

switch (theSection) {
    case 0:
        if (indexPath.row ==6) //the row you need to check for height (if any)

        {

            if([yourString length]<=35) // if less than 35 chars, just return a basic size of 50
            {
                return 50;
            }

            CGSize detailSize = [yourString sizeWithFont:[UIFont systemFontOfSize:20]constrainedToSize:CGSizeMake(270, 4000)lineBreakMode:UILineBreakModeWordWrap];

            return detailSize.height;
        }

hope this helps to help you understand that you can take a look at “sizeWithFont” to understand what it does to return the size.

+1
source

You need to use the method tableView:heightForRowAtIndexPath:in your UITableViewDelegate to return the row height. Note that this method should be very fast if you have more than a few rows, as it will be called for each row of the table, and not just for visible rows.

0
source

Source: https://habr.com/ru/post/1795541/


All Articles