-1

I followed UISwitch in a UITableView cell to put a UISwitch inside a tableview. Here is the code:

UISwitch *mySwitch = [[UISwitch alloc] init];
cell.accessoryView = mySwitch;

But the problem is that when I put the table into editing mode:

self.tableView.editing = YES;

The UISwitch dissapears.

Do you know how can I go around this issue?

Community
  • 1
  • 1
Jan
  • 2,462
  • 3
  • 31
  • 56

1 Answers1

2

Add UISwitch to contentView of cell.

The contentView of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the contentView so they will be positioned appropriately as the cell transitions into and out of editing mode.

[[cell contentView] addSubview:switch];
Bartosz Ciechanowski
  • 10,293
  • 5
  • 45
  • 60
abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
  • Ok, this answered my question, so I'll give it to you when I can (I have to wait 7 more minutes). I didn't know about the contentView. Thanks! – Jan Aug 26 '13 at 06:47
  • Hey @abhishekkharwar can you have a look at this question: http://stackoverflow.com/questions/18438459/how-to-properly-position-a-uitableview-cell-subview-to-the-right which follows your advice but I ran into another problem? Thanks! – Jan Aug 26 '13 at 07:07
  • 1
    did you try to add autoresizingmask on uiswitch ?? try to add UIAutoresizingMaskLeft and then let me know – abhishekkharwar Aug 26 '13 at 07:42
  • That's exactly what they told me on that question. you know your stuff! ;) Thanks again! – Jan Aug 26 '13 at 07:43