【发布时间】:2015-12-08 10:56:30
【问题描述】:
我打算使用自定义开关来控制在 tableview 中以编程方式设计的所有开关。我尝试使用自定义Switch的控制事件来控制所有的开关,但还是不行。我已经在 VC 的实现中为 tableview 声明了 UISwitch。
@implementation ViewController
{
UISwitch *switchObj;
NSArray *infoDetail;
}
@synthesize customSwitch;
- (void)viewDidLoad {
[super viewDidLoad];
infoDetail = [NSArray arrayWithObjects:@"Main door",@"Main door 2",@"Main windows",nil];
[customSwitch addTarget:self action:@selector(stateChanged:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
}
- (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];
}
cell.textLabel.text = [infoDetail objectAtIndex:indexPath.row];
switchObj = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)];
switchObj.transform = CGAffineTransformMakeScale(1, 1);
[switchObj addTarget:self action:@selector(alarmOn:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
cell.accessoryView = switchObj;
return cell;
}
- (void)stateChanged:(UISwitch *)switchState{
if (customSwitch.isOn) {
switchObj.enabled = YES;
}
else{
switchObj.enabled = NO;
}
}
- (void)alarmOn:(id)sender{
if ([sender isOn]) {
NSLog(@"Switch toggled on");
}else{
NSLog(@"Switch toggled off");
}
}
【问题讨论】:
-
你遇到了什么问题。此外,您只添加了开关,还没有提供任何类型的标签或其他东西来处理它们。
标签: objective-c uitableview uiswitch