开发者

How to add switch in tableview cells

My application navigation based and view is UITableviewController. I need t开发者_开发百科o add switch control for ON and OFF for particular UIViewtable cell. How to add.


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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜