【问题标题】:how to get selected UISwitch tag from UITableView sections?如何从 UITableView 部分获取选定的 UISwitch 标签?
【发布时间】:2020-03-21 20:55:25
【问题描述】:

如何从多个部分获取 UISwitch 标记。我有 3 个部分,用户可以更改开关状态。基于此,我想获得那个开关标签和部分索引。

【问题讨论】:

  • 您的问题含糊不清。请添加更多信息和一些代码。
  • 显示你尝试过的代码

标签: ios objective-c xcode uitableview cocoa-touch


【解决方案1】:

你可以提前设置

switch.accessibilityValue = "\(indexPath.section)"

然后回来

let section = Int(cell.accessibilityValue ?? "0")

【讨论】:

    【解决方案2】:
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
       UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
       UISwitch *onoff = [[UISwitch alloc] initWithFrame: CGRectMake(0, 0, 100, 50)];
       [onoff addTarget: self action: @selector(flip:) 
       forControlEvents:UIControlEventValueChanged];
       onoff.tag = section;
       [view addSubview: onoff];
    }
    - (IBAction)flip:(id)sender 
    { 
       if(sender.tag == 0)
        {
          //Do whatever you want to do.
        }
        else if(sender.tag == 1){
          //You can add as many as you want.
        }
    }
    

    您还需要了解有关表格视图中标题的另一件事,即覆盖标题高度。你可以这样做:

    - (CGFloat)tableView:(UITableView *)tableView 
       heightForHeaderInSection:(NSInteger)section
    {
       return 50;
    }
    

    【讨论】:

      【解决方案3】:

      这就是答案,它对我有用....

      UITableViewCell *cell = (UITableViewCell *)mySwitch.superview.superview;
      NSIndexPath *indexpath = [_deviceRulesTableView indexPathForCell:cell];
      NSLog(@"toggle section %ld rowID %ld", (long)indexpath.section, (long)indexpath.row);
      
      if (indexpath.section == 1) {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多