我怎样才能嵌入一个UISwitch
on a UITableView
细胞?示例可以在设置菜单中查看。
我当前的解决方案:
UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;
将其设置为accessoryView 通常是正确的方法。您可以将其设置为tableView:cellForRowAtIndexPath:
当开关被翻转时,您可能想使用目标/动作来做一些事情。就像这样:
- (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" );
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)