您的位置:首页 > 其它

在tableviewcell里面嵌入switch控件以及如何获取switch控件数据

2015-09-18 14:33 417 查看
主要是通过cell.accessoryView来添加switch控件
- (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];
//add a switch
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchview;
[switchview release];
}

cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];

return cell;
}

.h文件中添加:
- (IBAction) updateSwitchAtIndexPath:(id) sender;

获取switch数据:
- (IBAction)updateSwitchAtIndexPath:(id)sender {

UISwitch *switchView = (UISwitch *)sender;

if ([switchView isOn])
{
//do something..
}
else
{
//do something

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: