UISimpleTableViewCell and accessoryView

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

  1. macrumors newbie

    Joined:
    Mar 23, 2008
    #1
    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. macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #2
    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. macrumors regular

    Joined:
    Jul 20, 2007
    #3
    Hi!

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

    ~Matt

     
  4. thread starter macrumors newbie

    Joined:
    Mar 23, 2008
    #4
    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. macrumors newbie

    Joined:
    Mar 28, 2008
    #5
    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,

    Greg
     

Share This Page