When doing tableview, we sometimes need to adjust according to the cell's high dynamic, recently saw a code on the Internet good, with everyone share.
In
-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
Gets the height of the cell in the class:
Cgsize boundsize = Cgsizemake (216, Cgfloat_max); Cell.textLabel.text = @ "12345678900123456789"; cell.userinteractionenabled = NO; Cell.textLabel.numberOfLines = 0; Cgsize requiredsize = [Cell.textLabel.text sizewithfont:[uifont systemfontofsize:13] Constrainedtosize:boundsize Linebreakmode:uilinebreakmodewordwrap]; CGRect rect = cell.frame; Rect.size.height = requiredsize.height+5; Cell.frame = rect;
The height of the cell is obtained, and then the
-(cgfloat) TableView: (uitableview *) TableView Heightforrowatindexpath: (nsindexpath *) Indexpath {
Class to change the cell's height:
UITableViewCell *cell = [self Tableview:tableview cellforrowatindexpath:indexpath]; NSLog (@ "Cell height%f", cell.frame.size.height); return cell.frame.size.height;
Since then, the cell's height has changed automatically based on the contents of the label in the cell.
The main starting point is that I have a label, and then I'm going to show this label, and I'll get a height based on the size of the font and the number of rows, so the cell's height is there.