iphone - UITextView inside UITableViewCell -
according tutorial, had uitextview
inside uitableviewcell
. when uitableview
has been loaded , want uitextview
, uitableviewcell
resize height.
but did based on tutorial, uitextview
can not display content , part of uitextview
content still needed scroll display.
here's code:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *contenttableidentify = @"contenttableidentify"; uitableviewcell *cell = nil; //uilabel *label = nil; uitextview *textview = nil; cell = [self.tableview dequeuereusablecellwithidentifier:contenttableidentify]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:contenttableidentify]; textview = [[uitextview alloc] initwithframe:cgrectzero]; [textview settextcolor:[uicolor blackcolor]]; [textview setfont:[uifont fontwithname:@"helvetica" size:12.0f]]; [textview setbackgroundcolor:[uicolor clearcolor]]; [textview settag:1]; [textview seteditable:no]; [[cell contentview] addsubview:textview]; } nsstring *text = [self.contentarray objectatindex:[indexpath row]]; cgsize constraint = cgsizemake(320 - (10 * 2), 99999.0f); cgsize size = [text sizewithfont:[uifont systemfontofsize:12.0f] constrainedtosize:constraint linebreakmode:nslinebreakbywordwrapping]; if (!textview) textview = (uitextview *)[cell viewwithtag:1]; [textview settext:text]; [textview setframe:cgrectmake(10, 10, 320 - (10 * 2), max(size.height, 44.0f))]; return cell; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { nsstring *text = [self.contentarray objectatindex:[indexpath row]]; cgsize constraint = cgsizemake(320 - (10 * 2), 20000.0f); cgsize size = [text sizewithfont:[uifont systemfontofsize:12] constrainedtosize:constraint linebreakmode:nslinebreakbywordwrapping]; cgfloat height = max(size.height, 44.0f); return height + (10 * 2); }
you need factor in padding of text view when calculating height of cell.
if following:
// add 16px (8+8) account fr uitextview padding cgsize constraint = cgsizemake(320 - (10 * 2) -16, 99999.0f); gsize size = [text sizewithfont:[uifont systemfontofsize:12.0f] constrainedtosize:constraint linebreakmode:nslinebreakbywordwrapping] cgfloat height = max(size.height, 44.0f); return height + (10 * 2) + (8*2);
you'll find more information on question
Comments
Post a Comment