HeightForRowAt and estimatedHeightForRowAt in iOS

HeightForRowAt and estimatedHeightForRowAt in iOS



I have 2 tableview in one ViewController. Cell height of both tableview is different.



In first tableview am calculating via heightForRowAt delegate method because in that table content of a cell is static. In second via estimatedHeightForRowAt delegate method because in that table content of a cell is text(that may be small or large).


heightForRowAt


estimatedHeightForRowAt



So the problem is the cell height second tableview fixed 58. That is not increasing while large text display.


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
if tableView == tblHistory
let h = ((tableView.frame.size.width - 16) * 7 / 5) + 74
return h

else
return 58



func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
if tableView == tblHistory
let h = ((tableView.frame.size.width - 16) * 7 / 5) + 74
return h

else
return 58




Notes: I have properly set all constraints to resize cell height depends on text.





try to return UITableViewAutomaticDimension instead of 0
– Taras Chernyshenko
Aug 27 at 7:25


UITableViewAutomaticDimension


0





If your constraints are set properly, then you don't need to calculate the height. Just return UITableViewAutomaticDimension for both the table views.
– hardik parmar
Aug 27 at 7:45


UITableViewAutomaticDimension





@hardikparmar you are right.
– Pramod Tapaniya
Aug 27 at 8:21




3 Answers
3



the problem is the height of text cell fixed. That is not increasing while large text display.



You really have to have perfection in your constraints within the cell on the storyboard.



There is really a lot to know. You would have to show (screenshot) the storyboard of the cell, and show all the vertical constraints linking the UILabel and the other items.


UILabel



Generally speaking you should not be "fixing" the height of the UILabel, or the overall "chain" that includes the UILabel. If you do that correctly, it's implicit height will work.





Let me put a tip here: When creating cells with automatic dimensions in storyboard you should first put a "panel" view on the cell which should contain all your other views. This panel should have leading, trailing, top and bottom constraint put to superview at 0 but the bottom constraint priority should be set to 100. Your cell will never resize in your storyboard but now you can see your panel resizing correctly when you modify any parameters so you can debug your autosizing.
– Matic Oblak
Aug 27 at 7:28





Exactly, that is one definite approach to learning the "art of autolayout".
– Fattie
Aug 27 at 7:33





personally i recommend "stack view everywhere" :)
– Fattie
Aug 27 at 7:33



If your constraints are set properly, then you don't need to calculate the height. Just return UITableViewAutomaticDimension for both the table views.


UITableViewAutomaticDimension





Problems were not in second tableview constrains I have set all constraints perfectly and that was work perfectly also. Problem in first tableview. That is i was calculating height programmatically instaded of constraints.
– Pramod Tapaniya
Aug 27 at 9:02






Then you should show a screenshot of storyboard with the constraints so we can correct it.
– hardik parmar
Aug 27 at 9:38



Best solution is calculating height for both texts with the same method in heightForRowAt:


func heightForText(string: String, width: CGFloat, font: UIFont) -> CGFloat
let attrString = NSAttributedString(
string: string,
attributes: [NSFontAttributeName: font])
let size = attrString.boundingRect(
with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
return size.height





however, this is usually not a good approach - you should be using autolayout and constraints.
– Fattie
Aug 27 at 7:32





Please explain why 'this is usually not a good approach'. My solution doesn't excluding autolayout.
– Konrad Piękoś
Aug 27 at 8:31





hi Konrad , my point was: there is no reason to find the height of that element. (Indeed, there is no reason to find the height of any element.) Use autolayout alone and it will size everything.
– Fattie
Aug 27 at 9:46





If you are using autolayout in tableView (I mean 'UITableViewAutomaticDimension' for rowHeight) it will smash your performance for more complex layout. That's why I think that better solution is calculating height for entire cell and return it in heightForRowAt. I didn't say that this is the only one solution.
– Konrad Piękoś
Aug 27 at 10:27





i have already mention i am using constraints in app. then why i should have to write hard code. I was trying via hard code that was my mistake.
– Pramod Tapaniya
Aug 27 at 11:38






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế