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:
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
Post a Comment