0

I'm working with Tableview in Swift Now I got an issue with tableview cell height .I'm working on the chat Screen so I have text, Image. so I took one prototype cell within that I'm managing the screen. Now below is my Code for Height

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if msg_type == "Image"  {
        return 150
    }else if msg_type == "Text"{
        return UITableViewAutomaticDimension

    }
}

so text cell not getting as I expected, But here I don't know where is my mistake, and one more thing if I return UITableViewAutomaticDimension it is working fine for Text messages, not images. Please help me thanks in advance.

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70

1 Answers1

0

Make height dynamic like this

1- put this in viewDidLoad

     self.tableView.rowHeight = UITableViewAutomaticDimension;
     self.tableView.estimatedRowHeight = 150.0;

2- before return cell in cellForRow

    cell.layoutSubviews()

    cell.layoutIfNeeded()

3- hook constraints properly from top to bottom in cell xib or storyboard prototype

so , whether it's a cell containing a fixed height image like what you want 150 Or a multi line text it will be layouted properly

4- don't implement heightForRowAt

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • 1
    You don't need to call the layoutSubviews in cellForRow and would only need to cell layoutIfNeeded if it's required for something like an animation. – Upholder Of Truth Jan 05 '18 at 13:58
  • I do need as in complex layout cells the cell may encounter wrong height calculation , doing this solves the problem with me – Shehata Gamal Jan 05 '18 at 14:07
  • Well for the layoutSubviews direct from the Apple documentation 'You should not call this method directly. If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method.' So it you do need to call it directly you are doing something wrong. The layoutIfNeeded is only required if you want the layout immediately for some other calculation. Otherwise it is all handled automatically. – Upholder Of Truth Jan 05 '18 at 14:08
  • layoutSubviews redraws the object whether it needs layout or not , while layoutIfNeeded checks first , and as you may expect it's always there is a constraint configuration in cellForRow , so calling layoutSuvviews ensures completing the process and apending layoutIfNeeded for any issue handling – Shehata Gamal Jan 05 '18 at 14:20
  • layoutSubviews draws nothing it just calculates the layout. It also does nothing unless you override it (straight from the Apple documentation). So unless you have overridden it then nothing happens. You should call setNeedsLayout and then layoutIfNeeded it you want everything to be flagged for recalculation and then recalculated immediately. It's probably not causing any harm calling it direct but I try to avoid going against what the documentation specifically says. – Upholder Of Truth Jan 05 '18 at 14:23
  • Hi @Sh_Khan as you said I did , still it is showing same...I mean what is the problem with heightForRowAt it works fine in objective c. – Bittoo Praveen Jan 08 '18 at 06:06
  • you set constraints properly in cell xib from top to bottom ?? – Shehata Gamal Jan 08 '18 at 07:52
  • Thanks But not working ,I taken constraints as you said top to bottom – Bittoo Praveen Jan 11 '18 at 13:19
  • hooked bottom constraint of bottom most item to content view] – Shehata Gamal Jan 11 '18 at 13:23