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.
Asked
Active
Viewed 9,626 times
1
-
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 Answers
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.
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