ios - how to set table cell height dynamically depending on text label length? -


i trying learn obj-c , ios programming, still new. decided try , make simple reddit client application. trying display front page posts within uitableview, each post represented own cell.

on cell, here how setting title:

cell.textlabel.numberoflines = 0; cell.textlabel.text = [[tempdictionary objectforkey:@"data"] objectforkey:@"title"];    [cell.textlabel sizetofit]; 

however, cell ends clipping if title text long. here's picture:

enter image description here

how can make cells automatically adjust heights accommodate longer title labels, without intersecting other cells or detail text label?

thank help!

for new ios 8 there new way make simple.

there new parameter calculates cell height respect of auto layout constraints. uitableviewautomaticdimension. can use in viewwillappear method of view controller.

objective c:

- (void)viewwillappear:(bool)animated {   [super viewwillappear:animated];   self.tableview.estimatedrowheight = 70.0; // example. set average height    self.tableview.rowheight = uitableviewautomaticdimension;   [self.tableview reloaddata]; } 

swift:

override func viewwillappear(animated: bool) {     self.tableview.estimatedrowheight = 70 // example. set average height      self.tableview.rowheight = uitableviewautomaticdimension     self.tableview.reloaddata()  }  

work nice , want @ example. me, add height things in viewdidload , left in viewwillappear reloaddata(). there useful source.

from documentation: the default value of rowheight uitableviewautomaticdimension. leave code is, now, keep in mind don't need set row height in ios 8+.

the main thing need set constraints explicitly e.g. leading, trailing, top , bottom. try first, without adding lines of code above.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -