How to change cell height dynamically in UITableView static cell

前端 未结 4 612
盖世英雄少女心
盖世英雄少女心 2021-01-12 07:42

I am using UITableViewController instead detailView to show one entity details. I populated one row of data from PFQuery in my viewDidLoad method.

4条回答
  •  -上瘾入骨i
    2021-01-12 08:27

    Below is how I do.

    // this will set height of the row
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
       String *yourLongString = "Long text here";
       UILabel *mLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,30)];
                                                                       your size will change here
       mLabel.hidden = YES;
       mLabel.text = yourLongString;
       [mLabel sizeToFit];
               ^^^^^^^^ this is very important
    
       return mLabel.frame.size.height;
    }
    

    Now in cellForRowAtIndexPath adjust the height of actual label as per the height of the cell.

    Let me know if you are not clear.

提交回复
热议问题