1

My application navigation based and view is UITableviewController. I need to add switch control for ON and OFF for particular UIViewtable cell. How to add.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
SOF
  • 437
  • 2
  • 9
  • 19
  • possible duplicate of [UISwitch in a UITableView cell](http://stackoverflow.com/questions/3770019/uiswitch-in-a-uitableview-cell) – Mac May 17 '12 at 02:19

2 Answers2

3

Also a classic, this question has been asked a lot here already. For example, see this question. Search for UITableView and UISwitch to get way more results.

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
3

You can add a switch in the -tableView:cellForRowAtIndexPath: method found in the UITableViewDataSource Protocol

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    UISwitch *aSwitch = [[[UISwitch alloc] init] autorelease];
    [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [cell.contentView addSubview:aSwitch];
    return cell;
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178