如何在tableview单元格中添加开关

时间:2010-12-15 16:29:15

标签: iphone cocoa-touch uitableview

我的应用程序导航和视图是UITableviewController。我需要为特定的UIViewtable单元添加ON和OFF的开关控制。如何添加。

2 个答案:

答案 0 :(得分:3)

这也是一个经典之作,这个问题已经在这里问了很多。例如,请参阅this question。搜索UITableViewUISwitch以获得更多结果。

答案 1 :(得分:3)

您可以使用UITableViewDataSource Protocol

中找到的-tableView:cellForRowAtIndexPath:方法添加切换
- (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;
}