ios - UILabel height inside UITableViewCell -
i'm trying create tableview height of cells dynamic.
so far manage set height of cells depending on custom uilabel i've added inside.
with regular cell.textlabel works fine, when utilize own label goes wrong. see half label, when scroll , down, label extends , shows text... can see label should end in image.
image
this text within cellforrowatindexpath
:
static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // configure cell. auto *carforcell = [cars objectatindex:indexpath.row]; uilabel *namelabel = [[uilabel alloc] init]; namelabel = (uilabel *)[cell viewwithtag:100]; namelabel.numberoflines = 0; namelabel.text = carforcell.directions; [namelabel sizetofit]; [namelabel setbackgroundcolor:[uicolor greencolor]]; homecoming cell;
unless have typos in code posted, don't seem adding label cell @ all. seem creating new label every time, , replacing contents of namelabel
pointer cell's view (which nil).
try doing first , see how looks:
static nsstring *cellidentifier = @"cell"; uilabel *namelabel; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; namelabel = [[uilabel alloc] init]; namelabel.tag = 100; namelabel.numberoflines = 0; [namelabel setbackgroundcolor:[uicolor greencolor]]; [cell.contentview addsubview:namelabel]; } else { namelabel = (uilabel *)[cell viewwithtag:100]; } // configure cell. auto *carforcell = [cars objectatindex:indexpath.row]; namelabel.text = carforcell.directions; [namelabel sizetofit]; homecoming cell;
you need tell tableview size each cell needs using tableview:heightforrowatindexpath:
delegate method. mean getting relevant car
object 1 time again , calculating height using sizewithfont:sizewithfont:forwidth:linebreakmode:
ios objective-c xcode uitableview
No comments:
Post a Comment