【问题标题】:Detect Change in UISegmentedControl inside UITableView HeaderView检测 UITableView HeaderView 中 UISegmentedControl 的变化
【发布时间】:2017-05-21 20:50:38
【问题描述】:

我正在尝试根据 UITableView 标头中的UISegmentedControlselectedSegmentIndex 来控制我的UITableView。本质上,我希望UISegmentedControl 像 Twitter 的“我”标签一样工作。我在UITableView 标头中有UISegmentedControl,它使用此方法出列:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "sectionDetailsHeaderView") as! SectionDetailsHeaderTableViewCell
    return cell
}

我有一个连接到SectionDetailsHeaderTableViewCell 的 UISegmentedControl 插座,但我不知道如何检测 UISegmentedControl 的变化。我想在每次值更改时将变量var segmentedControlValue = Int() 设置为selectedSegmentIndex,并在值更改时调用函数func chooseDataToDisplay() {}。我该怎么做?

【问题讨论】:

  • 没有看到更多你的代码,也许你的模型已经检测到选择的段,但是MVC的视图端需要相应地更新。我现在正在与这件事搏斗。
  • 在下面查看我的答案!

标签: ios swift uitableview uisegmentedcontrol uitableviewsectionheader


【解决方案1】:

在@Schomes 回答和this post 的帮助下,我能够弄清楚!

1) 将UISegmentedControl 添加到它自己的UITableViewCell 中。我建议在UISegmentedControl 后面添加一个带有白色背景的UIView,覆盖整个UITableViewCell,以便TableView 单元格在UISegmentedControl 后面流动。

2) 添加您的自定义单元类并将其连接到 UITableViewCell

3) 向您的自定义 UITableViewCell 类添加一个出口,例如 yourSegmentedControl。不要在自定义 UITableViewCell 类中添加操作。这是在第 4 步中以编程方式完成的。

4) 在UIViewControllerUITableViewController 类中,添加以下代码。

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "yourCellIdentifier") as! yourCustomCellClass
    header.yourSegmentedControl.selectedSegmentIndex = self.segmentedControlValue
    header.yourSegmentedControl.addTarget(self, action: #selector(self.getSegmentValue(sender:)), for: .valueChanged)
    return header
}

self.segmentedControlValue 应在 ViewController 类的顶部声明为 var segmentedControlValue = Int()self.getSegmentValue(sender:) 应声明为:

func getSegmentValue(sender: UISegmentedControl) {
    self.segmentedControlValue = sender.selectedSegmentIndex
}

5) 你还需要添加:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 45
}

这是标题的高度。在我的例子中,大小是 45

6) 您现在可以在您的ViewController 中的任何位置访问self.segmentedControlValue。当用户点击不同的片段时,它会更新。

【讨论】:

  • 最后 .. 我已经在单元格类中声明了操作并使用委托来实现结果,它正在工作但它有一个缺陷,然后你的回答让我一夜无眠.. 谢谢..
  • @AbhishekMitra 很高兴这有帮助!我刚刚更新了使用 dequeueReusableHeaderFooterView 而不是 dequeueReusableCell 的答案。重新加载 UITableView 时调用 dequeueReusableCell 可能会出现问题
  • 是的,我在看到你的答案之前使用的所有东西,只有我在我的代码中错过了header.yourSegmentedControl.selectedSegmentIndex = self.segmentedControlValue 这一行,最后你的回答帮助克服了它。再次感谢。我对你的问题和答案投了赞成票.. :)
【解决方案2】:
segmentedControl.addTarget(self, action: "chooseDataToDisplay:", forControlEvents: .ValueChanged)

segmentedControl 是您的 UISegmentedControl。这将在每次值更改时调用func chooseDataToDisplay(segment: UISegmentedControl) {}

参考文献:

https://developer.apple.com/reference/uikit/uisegmentedcontrol

分段控件的行为部分 https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html#//apple_ref/doc/uid/TP40012857-UISegmentedControl

【讨论】:

猜你喜欢
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多