iphone - Autoresizing of UILabels in a UITableViewCell -


i adding own uilabels contentview of uitableviewcell because need more control on layout default uitableviewcellstyles provide. in essence want detaillabel have priority on textlabel textlabel gets truncated.

i have following code in uitableviewcontroller:

- (uitableviewcell *)tableview:(uitableview *)tableview   cellforrowatindexpath:(nsindexpath *)indexpath {   static nsstring * const kcellidentifier = @"customcell";    uitableviewcell * cell =       [tableview dequeuereusablecellwithidentifier:kcellidentifier];    uilabel * titlelabel, * datelabel;    if(cell == nil)   {     cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault         reuseidentifier:kcellidentifier] autorelease];      cell.accessorytype = uitableviewcellaccessorydisclosureindicator;      titlelabel = [[[uilabel alloc] init] autorelease];     titlelabel.tag = ktitlelabeltag;     titlelabel.autoresizingmask = uiviewautoresizingflexiblewidth;      datelabel = [[[uilabel alloc] init] autorelease];     datelabel.tag = kdatelabeltag;     datelabel.textcolor = [uicolor bluecolor];     datelabel.autoresizingmask = uiviewautoresizingflexibleleftmargin;      [cell.contentview addsubview:titlelabel];     [cell.contentview addsubview:datelabel];   }    [self configurecell:cell atindexpath:indexpath];   return cell; }  - (void)configurecell:(uitableviewcell *)pcell   atindexpath:(nsindexpath *)pindexpath {   const float kheight = 44.0, kleftindent = 8.0, koverallwidth = 293.0,       kgap = 1.0;    uilabel * titlelabel, * datelabel;   titlelabel = (uilabel *)[pcell.contentview viewwithtag:ktitlelabeltag];   datelabel = (uilabel *)[pcell.contentview viewwithtag:kdatelabeltag];    nsstring * datetext = @"9:39 am";    // calculate size of datelabel   cgsize datesize = [datetext sizewithfont:[datelabel font]       constrainedtosize:cgsizemake(koverallwidth, kheight)];    const float datexpos = koverallwidth - datesize.width;   datelabel.frame = cgrectmake(datexpos, 0.0, datesize.width, kheight);   titlelabel.frame = cgrectmake(kleftindent, 0.0,       datexpos - kleftindent - kgap, kheight);    titlelabel.text = @"some potentially long text wrapped.";   datelabel.text = datetext;   pcell.contentview.backgroundcolor = [uicolor purplecolor]; } 

the code above produces incorrect results. when table view shown, looks figure 1) in this image of renderings.

so there unwanted gap on right of datelabels. (the purple background better visibility going on)

when dragging tableview in 2) in image, bounces , looks 3).

the first row has layout wanted , calculated in configurecell:atindexpath:. guess behavior happens because cells re-used , configured again.

so feels missing kind of initialization, have tried calling setneedslayout , layoutsubviews of pcell , pcell.contentview never achieved initial correct rendering.

only when set autoresizingmask of titlelabel , datelabel uiviewautoresizingnone correct initial rendering, swipe delete not work because delete button gets rendered on datelabel.

what have change in code cells rendered initially first cell in third picture?

thanks!

ps: have inlined pictures unfortunately not have enough reputation that.

a good, , maybe simpler way to:

first create custom uitableviewcell subclass can setup using interface builder. if "mytableviewcell" custom cell view initialize in cellforrowatindexpath this:

mytableviewcellclass *cell = (mytableviewcellclass *)[tableview dequeuereusablecellwithidentifier:@"mytableviewcellclass"] autorelease];  if (!cell) cell = [[[mytableviewcellclass alloc] initwithstyle:uitableviewcellstyledefault   reuseidentifier:@"mytableviewcellclass"] autorelease];  // call specific methods on cell pass information it, not display [cell setproperties:...]; 

then implement layoutsubviews method in custom uitableviewcell subclass. instance:

-(void) layoutsubviews { // must call first make sure cell gets current parent information [super layoutsubviews];  // retrieve content view bounds. include edit symbols when present (delete button , ordering symbol float inset = 5.0; cgrect bounds = [[self contentview] bounds];  // keep on going here own view layout.  } 

doing separate cell model (cellforrowatindexpath) cell view (your custom implementation of cell drawing). if later change implementation (layout) of cell can changing cell layout without worrying cellforrowatindexpath method.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -