以下是我禁用didselectRowAtIndexPath的代码。开关打开时,它禁用did select行
但当附件中的多个开关打开时,例如,如果有三个开关打开,而我关闭了一个开关,则did select行可以工作。
我想禁用它。
有什么帮助吗?

这是我的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (switchControl.on) {
NSLog(@"Switch is on");
UIAlertView *switchAlert = [[UIAlertView alloc]initWithTitle:@"Switch is On" message:@"Please turn it off to proceed further" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[switchAlert show];
}
else{
// table view did select working .
}而cellForRowAtIndexPath是-
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"CellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.backgroundColor=[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];
switchControl = [[UISwitch alloc] initWithFrame:CGRectZero];
switchControl.tag = indexPath.row;
cell.accessoryView = switchControl;
[switchControl setOn:NO animated:NO];
[switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
}}发布于 2015-06-25 13:56:45
让阵列了解开关是否打开
然后使用这个委托方法
- (NSIndexPath * nullable)tableView:(UITableView * nonnull)tableView
willSelectRowAtIndexPath:(NSIndexPath * nonnull)indexPath如果此indexPath的开关已打开,则返回nil
来自文档
返回值
确认或更改选定行的索引路径对象。如果希望选择另一个单元格,则返回indexPath以外的NSIndexPath对象。如果不希望选择行,则返回nil。
发布于 2015-06-25 13:52:32
我想问题出在你的switchControl。您必须创建与每个单元格相对应的开关控件。现在,从你的代码看,你似乎是在为所有的行创建通用的.The,开关控件的虚拟代码应该如下所示:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
if (cell.switchControl.on) {
NSLog(@"Switch is on");
}
else{
}https://stackoverflow.com/questions/31042039
复制相似问题