开发者

How to add switch in tableview cells

开发者 https://www.devze.com 2023-01-30 13:26 出处:网络
My application navigation based andview 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 h

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

精彩评论

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