ios - How to resize UITableViewCell to fit its content? -
i have uitableview
multiple reusable tableviewcells. in 1 cell have uitextview
, resizes fit content. "just" have resize contentview
of tableviewcell, can read while text. tried:
cell2.contentview.bounds.size.height = cell2.discriptiontextview.bounds.size.height;
or:
cell2.contentview.frame = cgrectmake(0, cell2.discriptiontextview.bounds.origin.y, cell2.discriptiontextview.bounds.size.width, cell2.discriptiontextview.bounds.size.height);
in method:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {}
but won't work.
does know how this?
new code:
@implementation appdetail cgfloat height; … - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {… cell2.textview.text = self.text; [cell2.textview sizetofit]; height = cgrectgetheight(cell2.textview.bounds); … } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { if (indexpath.row == 0) { return 143; } if (indexpath.row == 1) { return height; } return 0; }
you need implement heightforrowatindexpath
.
say data displayed in textview stored in nsarray.
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { cgfloat cellheight = 30; //assuming textview's origin.y 30 , textview last ui element in cell nsstring *text = (nsstring *)[textarray objectatindex:indexpath.row]; uifont *font = [uifont systemfontofsize:14];// font should same of textview cgsize constraintsize = cgsizemake(maxwidth, cgfloat_max);// maxwidth = max width textview cgsize size = [text sizewithfont:font constrainedtosize:constraintsize linebreakmode:uilinebreakmodewordwrap]; cellheight += size.height; //you can add cell padding if want space below textview }
Comments
Post a Comment