首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当tableview附件类型的开关打开时,如何禁用didselectrowAtIndexPath。?

当tableview附件类型的开关打开时,如何禁用didselectrowAtIndexPath。?
EN

Stack Overflow用户
提问于 2015-06-25 13:40:27
回答 2查看 873关注 0票数 1

以下是我禁用didselectRowAtIndexPath的代码。开关打开时,它禁用did select行

但当附件中的多个开关打开时,例如,如果有三个开关打开,而我关闭了一个开关,则did select行可以工作。

我想禁用它。

有什么帮助吗?

这是我的代码。

代码语言:javascript
复制
     - (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是-

代码语言:javascript
复制
   -(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];
}}
EN

回答 2

Stack Overflow用户

发布于 2015-06-25 13:56:45

让阵列了解开关是否打开

然后使用这个委托方法

代码语言:javascript
复制
- (NSIndexPath * nullable)tableView:(UITableView * nonnull)tableView
       willSelectRowAtIndexPath:(NSIndexPath * nonnull)indexPath

如果此indexPath的开关已打开,则返回nil

来自文档

返回值

确认或更改选定行的索引路径对象。如果希望选择另一个单元格,则返回indexPath以外的NSIndexPath对象。如果不希望选择行,则返回nil。

票数 2
EN

Stack Overflow用户

发布于 2015-06-25 13:52:32

我想问题出在你的switchControl。您必须创建与每个单元格相对应的开关控件。现在,从你的代码看,你似乎是在为所有的行创建通用的.The,开关控件的虚拟代码应该如下所示:

代码语言:javascript
复制
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
  if (cell.switchControl.on) {
   NSLog(@"Switch is on");
   }
    else{
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31042039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档