iOS8: Possible to use “tableview.rowHeight = UITableViewAutomaticDimension” for static cells?

前端 未结 8 979
慢半拍i
慢半拍i 2020-12-14 17:00

I have a UITableView that has five static cells. I need the cell height of one cell to adjust automatically to its contents, which is one UILabel.

Is there any way I

相关标签:
8条回答
  • 2020-12-14 17:45

    I was looking for an answer to this as well and found this solution, which i tested and works perfectly in iOS8.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row == 2)  //Dynamic Height
        {
            return UITableViewAutomaticDimension;
        }
        else 
        {
            return 50;  // Static Height
        }
    }
    
    0 讨论(0)
  • 2020-12-14 17:46

    For iOS 8, I found out that you cannot do the same strategy as for Dynamic Table View Cell.

    To automatically adjust the height of static cells, I implement these two UITableViewDelegate methods:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewAutomaticDimension;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 44;
    }
    

    Hope it helps.

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