UISimpleTableViewCell and accessoryView

Discussion in 'Mac Programming' started by PeyloW, Mar 23, 2008.

  1. PeyloW
    Expand Collapse
    macrumors newbie

    Mar 23, 2008
    I have tried to add a UISwitch as an accessory view to one of the rows in my table.

    My problem is that the accessory view is invisible. By initializing the UISwitch with the CGRectZero frame I can get the UISipleTableViewCell's text label to truncate, so something is there :). I have also tried to override acceroryRectForBounds:, but with no success.

    If I use the inherited addSubview: from UIView the UISwitch do show up. But I do not think this is the right way to do it.

    Has anyonw successfully used an accessory view with a UISimpleTableViewCell, or UITableViewCell?
  2. cmaier
    Expand Collapse
    macrumors G3

    Jul 25, 2007
    Why not just use addSubview?

    Anyway, I haven't tried this. Did you check in the debugger to see if loadView is getting clled on the UISwitch?
  3. Matt342
    Expand Collapse
    macrumors regular

    Jul 20, 2007

    I'm having the same problem. I cannot get it to show. Can you post the code that you used to show the UISwitch?


  4. PeyloW
    Expand Collapse
    thread starter macrumors newbie

    Mar 23, 2008
    This is the method I use to create my table cells:

    - (UITableViewCell *)tableViewCellWithLabel:(NSString *)label text:(NSString *)text
    UISimpleTableViewCell *tableCell = [[[UISimpleTableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    tableCell.text = label;

    UISwitch *aswitch = [[[UISwitch alloc] initWithFrame:CGRectMake(200, 7, 100, 30)] autorelease];
    //[tableCell addSubview:aswitch]; // This one works!
    tableCell.accessoryView = aswitch; // This one do not!

    return tableCell;
  5. gregjmclaughlin
    Expand Collapse
    macrumors newbie

    Mar 28, 2008
    Try using both calls. In other words:

    [tableCell addSubview:aswitch];
    tableCell.accessoryView = aswitch;

    as a work around. This works for me. You can also change your initWithFrame: to use CGRectZero as the above combination will make the default sized switch and position it correctly.

    I don't know that it is intended that you need the addSubview ... but at least for now you seem to.

    Hope that helps,


Share This Page