1
我需要在表格视图部分标题中添加清除按钮。它和通知中心的完全一样。所以当用户点击它时,'x'将变为'清除'。如果他/她再次点击它,应用程序将执行操作。但任何点击此按钮外的'清除'将返回'x'iOS UITableView:清除部分按钮
我该如何做到这一点?在此先感谢
我需要在表格视图部分标题中添加清除按钮。它和通知中心的完全一样。所以当用户点击它时,'x'将变为'清除'。如果他/她再次点击它,应用程序将执行操作。但任何点击此按钮外的'清除'将返回'x'iOS UITableView:清除部分按钮
我该如何做到这一点?在此先感谢
不幸的是这不来了开箱即用。您必须创建您自己的自定义节标题视图。该自定义标题视图在左侧包含一个UILabel。在右侧放置一个标题为“X”的UIButton
。当用户点击按钮时,将其标题更改为“清除”并将其状态更改为.Selected
。只有清除表视图部分当按钮被窃听和它的状态是.Selected
:
func setupButton() {
let button = UIButton()
button.setTitle("X", forState: .Normal)
button.addTarget(self, action: Selector("didPressButton:"), forControlEvents: .TouchUpInside)
addSubview(button)
}
func didPressButton(button: UIButton) {
if button.selected {
// clear your table view section
}
button.setTitle("Clear", forState: .Normal)
button.selected = true
}
,以便将按钮回到其未选中状态添加背景UIView
到您的节标题视图,并添加一个UITapGestureRecognizer
它:
func setupBackground() {
let background = UIView()
let recognizer = UITapGestureRecognizer(target: self, action: Selector("didTapOutside:"))
background.addGestureRecognizer(recognizer)
insertSubview(background, belowSubview: button)
}
func didTapOutside(recognizer: UITapGestureRecognizer) {
button.selected = false
button.setTitle("X", forState: .Normal)
}
要使用您的自定义节头在你的表视图您实现以下UITableViewDelegate
方法:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// create your custom section header and return it
}
我明白了。我认为有一个简单的方法来做到这一点,但事实并非如此。谢谢 – Yacob