table 视图中的 UISwitch 无法通过自定义开关禁用
UISwitch in table view were not able to disabled by a custom switch
我打算使用自定义开关来控制在表视图中以编程方式设计的所有开关。我试过用自定义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");
}
}
尝试将 switchObj 声明代码放入 UITableView 的 cellForRowAtIndex
方法中并从顶部删除。
UISwitch *switchObj;
现在的方法就像
- (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];
UISwitch *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;
if (customSwitch.isOn) {
switchObj.enabled = YES;
}
else{
switchObj.enabled = NO;
}
return cell;
}
另外,当您更改状态自定义开关时,也要重新加载 tableView。
按如下方式更改您的 stateChanged
方法:
-(void)stateChanged:(UISwitch *)switchState{
[myTableView reloadData];
}
我打算使用自定义开关来控制在表视图中以编程方式设计的所有开关。我试过用自定义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");
}
}
尝试将 switchObj 声明代码放入 UITableView 的 cellForRowAtIndex
方法中并从顶部删除。
UISwitch *switchObj;
现在的方法就像
- (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];
UISwitch *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;
if (customSwitch.isOn) {
switchObj.enabled = YES;
}
else{
switchObj.enabled = NO;
}
return cell;
}
另外,当您更改状态自定义开关时,也要重新加载 tableView。
按如下方式更改您的 stateChanged
方法:
-(void)stateChanged:(UISwitch *)switchState{
[myTableView reloadData];
}