在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
}
}
- (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
}
}
相关文章推荐
- 改变UITextField的Placeholder颜色
- 常用工具下载链接
- 2015最火十大Android开源项目,是个程序员你就该看看!
- 冒泡排序Bubble sort-golang
- 类的构造方法调用几次的判定:
- Qt利用QProcess打开路径(含中文路径)的方法
- java多种方式读文件,追加文件内容,对文件的各种操作
- jquery扩展的radio和checkbox前端调用方法
- java学习之Collections工具类
- apache session 会话保持 mod_proxy_balancer
- 最近被困扰的一个问题
- bash编程之:流程控制
- JSONP理解
- Android Studio系列教程二--基本设置与运行
- windbg中的栈信息
- android极光推送
- JSON快速解析(Json和FastJson对比)
- ASCII
- Android内嵌html5添加原生微信分享的解决办法
- java mysql大数据量批量插入与流式读取分析