UISwitch - UITableView
UISwitch - UITableView
我有这个 tableView:
但我无法识别用户何时选择了该行中的 UISwitch。
我知道我需要 UITableViewCell class 中的@IBAction,但我没有找到适合我的指南。
我想在用户单击 UISwitch 时打印行的 indexPath。
这是我的 class 单元格:
class TicketsFilterCell: UITableViewCell {
@IBOutlet weak var textFilter: UILabel!
@IBOutlet weak var switchFilter: UISwitch!
class var reuseIdentifier: String? {
get {
return "TicketsFilterCell"
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我该怎么办?
提前致谢。
编辑
我的 cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: TicketsFilterCell? = tableView.dequeueReusableCellWithIdentifier("cellTicketFilter", forIndexPath: indexPath) as? TicketsFilterCell
cell?.delegate = self
if(indexPath.section == 0) {
let text = status[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 1) {
let text = queues [indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 2) {
let text = types[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 3) {
let text = severities[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
}
return cell!
}
次元,
这是您可以做的:)
在您的单元格中声明一个协议 class 让我们将其称为 CellProtocol。
protocol CellProtocol : class {
func switchButtonTapped(WithStatus status : Bool, ForCell myCell : TicketsFilterCell)
}
在您的 TicketsFilterCell 中声明一个变量 class 来保存委托,
weak var delegate : CellProtocol!
当用户点击你的开关时触发代理
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.delegate .switchButtonTapped(WithStatus: selected, ForCell: self)
}
在你的表中ViewController 使用
确认协议
class ViewController: UITableViewController,CellProtocol {
现在在您的 ViewController 的 cellForRoAtIndexPath 中,为每个单元格将委托设置为 self。
cell.delegate = self
最终实现委托方法如下
func switchButtonTapped(WithStatus status: Bool, ForCell cell: TicketsFilterCell) {
let indexPath = self.tableView .indexPathForCell(cell)
print("cell at indexpath \(indexPath) tapped with switch status \(status)")
}
编辑:
对于每个单元格,您都已将 IBOutlet 拖到开关 :) 现在您只需要从 UISwitch 到单元格的 IBAction :)
@IBAction func switchTapped(sender: UISwitch) {
self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self)
}
就是这样:)
使用这个
cell.yourSwitch.tag=indexPath.row;
在 cellForRowAtIndexPath 方法中
基于该标记在开关 on/off 操作中执行您的代码。
将 @IBAction
从开关添加到视图控制器本身。在 IBAction 函数中,将 sender
(UISwitch) 的位置转换为 tableView 的框架。然后,在 tableView.
中找到该点的单元格
我相信这是向 table 视图单元格添加操作的最优雅的方式。
- 标签 属性 的最大问题是它的使用一开始总是很简单,但随着时间的推移,逻辑(或缺乏逻辑)会演变成一团乱七八糟的东西。
- 这比创建协议和实施委托更容易
下面是IBAction
连接到开关的valueChanged
动作
@IBAction func switchValueChanged(sender: AnyObject) {
let switchButton = sender as! UISwitch
let buttonPosition = switchButton.convertPoint(CGPointZero, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)!
NSLog("switch at index %d changed value to %@", indexPath.row, switchButton.on)
}
我有这个 tableView:
但我无法识别用户何时选择了该行中的 UISwitch。 我知道我需要 UITableViewCell class 中的@IBAction,但我没有找到适合我的指南。 我想在用户单击 UISwitch 时打印行的 indexPath。
这是我的 class 单元格:
class TicketsFilterCell: UITableViewCell {
@IBOutlet weak var textFilter: UILabel!
@IBOutlet weak var switchFilter: UISwitch!
class var reuseIdentifier: String? {
get {
return "TicketsFilterCell"
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我该怎么办?
提前致谢。
编辑
我的 cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: TicketsFilterCell? = tableView.dequeueReusableCellWithIdentifier("cellTicketFilter", forIndexPath: indexPath) as? TicketsFilterCell
cell?.delegate = self
if(indexPath.section == 0) {
let text = status[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 1) {
let text = queues [indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 2) {
let text = types[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 3) {
let text = severities[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
}
return cell!
}
次元,
这是您可以做的:)
在您的单元格中声明一个协议 class 让我们将其称为 CellProtocol。
protocol CellProtocol : class {
func switchButtonTapped(WithStatus status : Bool, ForCell myCell : TicketsFilterCell)
}
在您的 TicketsFilterCell 中声明一个变量 class 来保存委托,
weak var delegate : CellProtocol!
当用户点击你的开关时触发代理
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.delegate .switchButtonTapped(WithStatus: selected, ForCell: self)
}
在你的表中ViewController 使用
确认协议class ViewController: UITableViewController,CellProtocol {
现在在您的 ViewController 的 cellForRoAtIndexPath 中,为每个单元格将委托设置为 self。
cell.delegate = self
最终实现委托方法如下
func switchButtonTapped(WithStatus status: Bool, ForCell cell: TicketsFilterCell) {
let indexPath = self.tableView .indexPathForCell(cell)
print("cell at indexpath \(indexPath) tapped with switch status \(status)")
}
编辑:
对于每个单元格,您都已将 IBOutlet 拖到开关 :) 现在您只需要从 UISwitch 到单元格的 IBAction :)
@IBAction func switchTapped(sender: UISwitch) {
self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self)
}
就是这样:)
使用这个
cell.yourSwitch.tag=indexPath.row;
在 cellForRowAtIndexPath 方法中
基于该标记在开关 on/off 操作中执行您的代码。
将 @IBAction
从开关添加到视图控制器本身。在 IBAction 函数中,将 sender
(UISwitch) 的位置转换为 tableView 的框架。然后,在 tableView.
我相信这是向 table 视图单元格添加操作的最优雅的方式。
- 标签 属性 的最大问题是它的使用一开始总是很简单,但随着时间的推移,逻辑(或缺乏逻辑)会演变成一团乱七八糟的东西。
- 这比创建协议和实施委托更容易
下面是IBAction
连接到开关的valueChanged
动作
@IBAction func switchValueChanged(sender: AnyObject) {
let switchButton = sender as! UISwitch
let buttonPosition = switchButton.convertPoint(CGPointZero, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(buttonPosition)!
NSLog("switch at index %d changed value to %@", indexPath.row, switchButton.on)
}