UITableView 单元格中的 UISwitch [英] UISwitch in a UITableView cell
本文介绍了UITableView 单元格中的 UISwitch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 UITableView
单元格上嵌入 UISwitch
?示例可以在设置菜单中看到.
How can I embed a UISwitch
on a UITableView
cell? Examples can be seen in the settings menu.
我目前的解决方案:
UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;
推荐答案
将其设置为附件视图通常是要走的路.您可以在 tableView:cellForRowAtIndexPath:
中进行设置,您可能希望在切换开关时使用目标/操作来执行某些操作.像这样:
Setting it as the accessoryView is usually the way to go. You can set it up in tableView:cellForRowAtIndexPath:
You may want to use target/action to do something when the switch is flipped. Like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch( [indexPath row] ) {
case MY_SWITCH_CELL: {
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
aCell.textLabel.text = @"I Have A Switch";
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
break;
}
return nil;
}
- (void)switchChanged:(id)sender {
UISwitch *switchControl = sender;
NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}
这篇关于UITableView 单元格中的 UISwitch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文