custom accesoryView pains

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

  1. tutiplain macrumors member

    Joined:
    Feb 4, 2011
    #1
    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:

    Code:
    -(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.textColor=color;
    	//label.font = font;
    	label.text = [desc.percent stringValue];
    	
    	cell.accessoryView=label;
    	
    	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 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Couple things.

    Make your fonts like this

    Code:
    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

    Joined:
    Feb 4, 2011
    #3
    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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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