custom accesoryView pains

Discussion in 'iOS Programming' started by tutiplain, Mar 16, 2011.

  1. tutiplain macrumors member

    Feb 4, 2011
    Hi everyone,

    I am trying something that seemed simple enough when I started, but has quickly complicated. I am simply trying to use a UILabel for a custom view in the accesoryView of a UITableViewCell.

    I set everything up like this in my data source:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    	static NSString *MyIdentifier = @"MyIdentifier";
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if (cell == nil) 
    		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    	Descuento *desc = [descuentos objectAtIndex:indexPath.row];
    	cell.textLabel.text = [desc getAsFinalCalculatedValue:[salario_bruto floatValue]];
    	//setea accesoryview
    	UILabel *label = [[UILabel alloc] init];
    	UIColor *color = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f];
    	//UIFont *font   = [[UIFont alloc] fontWithSize:70.f];
    	//label.font = font;
    	label.text = [desc.percent stringValue];
    	return cell;
    Based on examples I've seen in google searches, everybody seems to be doing this type of setup in viewDidLoad method, but since my datasource is a custom class, I don't have that method available. I've also seen people initialize a UILabel using initWithFrame:frame, but I have no idea where the "frame" object is supposed to come from. I've also seen people add the view to the contentView using the addView: method. I chose this approach since it's documented in Apple's docs on TableViews. Simply assign a new view to the accessoryView property, and there it is. However, when I run my app with this code, no UILabels are added to the right of my cells. Also, if I uncomment the UIFont lines, the app crashes.

    Can anyone tell me what I am doing wrong? Any help will be greatly appreciated.
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Couple things.

    Make your fonts like this

    UIFont* font = [UIFont systemFontOfSize:18];
    You do know that a font size of 70 is gigantic, don't you?

    Maybe you need to set a frame for your label or sizeToFit.

    Frankly I've never seen a label used as an accessory view. It's almost always a button. Could work I guess.
  3. tutiplain thread starter macrumors member

    Feb 4, 2011
    Oops! I meant to write 7.0f over there, maybe that's what's causing the crash. Still, shouldn't I be able to at least see the label? I'm gonna try to use the detailView label instead, and see what happens. If you have any other suggestions, they're welcome.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Two things:

    1) As PhoneyDeveloper alluded to, you need to set the frame of your label.

    2) You should probably consider not instantiating a new UILabel every time cellForRowAtIndexPath: is called, since it seems you intend this view to be used for every single row. (I suspect the way you are currently doing it should be causing some memory leaks). You should instantiate it only for nil cells and then just update the text for it at the same time as updating the text for textLabel. You'll probably need a way to keep a reference or be able to retrieve the label between those sections of code.

Share This Page