Swift 在tableview中选择和取消选择行时发出执行函数
注意:TableView是使用JSON填充的,其结构包含codeNum值 我需要执行两个不同的函数,这取决于是否选择了行,以下是我现有的选择机制:Swift 在tableview中选择和取消选择行时发出执行函数,swift,uitableview,Swift,Uitableview,注意:TableView是使用JSON填充的,其结构包含codeNum值 我需要执行两个不同的函数,这取决于是否选择了行,以下是我现有的选择机制: class CheckableTableViewCell: UITableViewCell { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier
class CheckableTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}
选择一行时,需要运行select()
,未选择的行时,需要运行unselect()
。在此之前,需要将rows codeNum值指定给变量tappedSelected:
structure = sections[indexPath.section].items
let theStructure = structure[indexPath.row]
tappedSelected = theStructure.codeNum
如何将其实现到自定义类中?您应该尝试使用didSelectRowAt()和didSelectRowAt()方法。假设这里允许多个选择,我会这样做 如果选择了单元格 如果未选择单元格
在选择/取消选择单元格时,可以使用闭包调用方法 首先,在您的
CheckableTableViewCell
中创建一个闭包
,并在setSelected(uuu:animated:)
方法中使用true/false
调用它,即
class CheckableTableViewCell: UITableViewCell {
var handler: ((Bool)->())? //here....
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
handler?(selected) //here....
}
//rest of the code....
}
class VC: UIViewController, UITableViewDataSource {
//rest of the code...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
cell.handler = {[weak self](selected) in
selected ? self?.select(indexPath) : self?.unselect()
}
return cell
}
func select(_ indexPath: IndexPath) {
let structure = sections[indexPath.section].items
let theStructure = structure[indexPath.row]
tappedSelected = theStructure.codeNum
}
func unselect() {
//add the code here...
}
}
接下来,在您的ViewController
中,为tableView(uu:cellForRowAt:)
方法中的CheckableTableViewCell
实例设置handler
的值,即
class CheckableTableViewCell: UITableViewCell {
var handler: ((Bool)->())? //here....
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
handler?(selected) //here....
}
//rest of the code....
}
class VC: UIViewController, UITableViewDataSource {
//rest of the code...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
cell.handler = {[weak self](selected) in
selected ? self?.select(indexPath) : self?.unselect()
}
return cell
}
func select(_ indexPath: IndexPath) {
let structure = sections[indexPath.section].items
let theStructure = structure[indexPath.row]
tappedSelected = theStructure.codeNum
}
func unselect() {
//add the code here...
}
}
这将解决在选中或取消选中单元格时调用select()
或unselect()
方法的问题
对于您的另一项要求,请说明您希望在什么时候将
codeNum
值设置为tappedSelected
。而tappedSelected
在哪里,在ViewController
或CheckableTableViewCell
中,哪里有select()和unselect()方法?在ViewController或TableViewCell中?tappedSelected是一个变量,一旦选定一行,该变量将包含codeNum的值。我在函数select()和unselect()中使用它。如果您有其他问题,请告诉我。在这种情况下,您可以编写与前面在select()
方法中所做的相同的代码。select()和unselect()都是指“structure=sections…”吗?在测试代码时,我还注意到unselect()在TableViewController首次运行时继续执行