【问题标题】:Managing UITableView with multiple segmented controls使用多个分段控件管理 UITableView
【发布时间】:2015-02-20 00:00:53
【问题描述】:

我正在努力做到这一点,以便如果任何分段控件发生更改,顶部的应用按钮就会启用。我还需要能够访问每个人的价值。我正在使用斯威夫特。这是它的样子:

感谢您的帮助。

【问题讨论】:

  • 请在您尝试实现此目的的地方发布代码...

标签: ios xcode uitableview swift uisegmentedcontrol


【解决方案1】:
I assume you must have subclass of your UITableViewCell. If you don't have make subclass of UITableViewCell

In subclass
1. Create as weak property of UISegementControl
2. Create protocol
protocol TableViewCellDelegate
{
 func segmentSelected(sender:UISegmentControl)
}
3. create delegate as weak refrence for protocol

In Interface Builder
1. link your UISegementControl to your UITableViewController
2. link your delegate to your UITableViewController

In UITableViewController

func segmentSelected(sender:UISegmentControl)
{
  // here you get value changed segment
}

【讨论】:

    【解决方案2】:

    当您设置单元格时,您可以为分段控件添加事件侦听器:

    [cell.segmentedControl addTarget:self
                         action:@selector(action:)
               forControlEvents:UIControlEventValueChanged];
    

    您可以通过下面方法中的参数来处理哪个分段控件发生了变化

    -(void)action:(UISegmentedControl*)sender
    

    如果您确实需要随时访问所有分段控件,那么也许您可以维护一个包含所有分段控件的字典,它们的键是 indexPath 或每行的其他一些唯一属性。然后,当您实例化单元格时,您在字典上设置分段控件

    编辑:对不起,我没有意识到你在使用 swift,我相信上面的代码应该是 swift 中的以下代码:

          cell.segmentedControl.addTarget(self, action: "action:", forControlEvents: .EventValueChanged)
    
    
    
          func action(sender:UISegmentedControl){}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2021-09-21
      相关资源
      最近更新 更多