How to change cell height dynamically in UITableView static cell

前端 未结 4 640
盖世英雄少女心
盖世英雄少女心 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条回答
  •  不要未来只要你来
    2021-01-12 08:06

    If you are using autolayout. Add top, left and right constrain in to your label.

    Then in heightForRowAtIndexPath create your cell

    static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    

    Set values to label and calculate size of the label after setting the value

        [cell layoutIfNeeded];
        return cell.label.frame.origin.y+cell.label.frame.size.height;
    

    This will give you the exact hight of the label and your cell height will be same

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
                cell.titleLabel.text=@"YOUR TEXT";
                [cell layoutIfNeeded];
                return cell.titleLabel.frame.origin.y+cell.titleLabel.frame.size.height;
    }
    

提交回复
热议问题