表格视图单元格被选中
我使用tableview与选定按钮,我对我的代码有一个问题,因为选择了按钮时,我不知道什么是我的单元格的索引路径.
I use TableView with selected button, I have a problem with my code because when the button is selected, I don't know what is the IndexPath of my cell.
我使用带有表格视图 cell.xib 的文件 TableView 单元格
I use a file TableView cell with table view cell.xib
@IBOutlet weak var lbTitle: UILabel!
@IBOutlet weak var lbDetail: UILabel!
@IBOutlet weak var btnCheckMark: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
在我的视图控制器中,我有这个:
In my View controller I have this :
override func viewDidLoad() {
super.viewDidLoad()
self.topBar.rightBarButtonItem = UIBarButtonItem(title: "Apply", style: .done, target: self, action: #selector(self.apply))
self.topBar.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "backButton"), style: .done, target: self, action: #selector(backHome))
listeView.register(UINib.init(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "CheckList")
listeView.dataSource = self
listeView.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return deviceData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let rowpath = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckList") as! TableViewCell
cell.lbTitle.text = "\(self.deviceData[indexPath.row].brandName) \(self.deviceData[indexPath.row].modelName)"
cell.lbDetail.text = "\(self.deviceData[indexPath.row].operatorName) \(self.deviceData[indexPath.row].version), \(self.deviceData[indexPath.row].browserName) \(self.deviceData[indexPath.row].version)"
//cell.selectionStyle = .none
cell.btnCheckMark.addTarget(self, action: #selector(checkMarkButton(sender:)), for: .touchUpInside)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
let test = TableViewCell()
test.lbTitle.text = "11"
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 71.0
}
@objc func checkMarkButton(sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
nb -= 1
//rint(paramaters)
} else {
sender.isSelected = true
paramaters["devicesList[\(nb)][deviceId]"] = id
nb += 1
}
print(paramaters)
}
在我的函数 checkMarkButton 中,我想知道 indexPath.row
In my function checkMarkButton, I want to know the indexPath.row
在 Swift 中最有效的方法是回调闭包.
In Swift the most efficient way is a callback closure.
这很简单:没有协议、没有目标/动作、没有委托、没有标签、没有索引路径、没有视图数学、没有 @objc
属性.
It's pretty easy: No protocols, no target/action, no delegate, no tags, no index paths, no view math, no @objc
attribute.
在表示
deviceData
的 model 中添加属性isSelected
var isSelected = false
在单元格中添加一个属性callback
和一个IBAction
.将按钮连接到 IBAction
.在 IBAction
中切换选择状态并调用回调
In the cell add a property callback
and an IBAction
. Connect the button to the IBAction
. In the IBAction
the selection state is toggled and the callback is called
var callback : ((UIButton) -> Void))?
@IBAction func buttonPressed(_ sender : UIButton) {
sender.isSelected.toggle()
callback?(sender)
}
在视图控制器中设置和处理cellForRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let rowpath = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckList") as! TableViewCell
let data = self.deviceData[indexPath.row]
cell.lbTitle.text = "\(data.brandName) \(data.modelName)"
cell.lbDetail.text = "\(data.operatorName) \(data.version), \(data.browserName) \(data.version)"
cell.btnCheckMark.isSelected = data.isSelected
cell.callback { button in
self.deviceData[indexPath.row].isSelected = button.isSelected
// or if deviceData is a class with reference semantics
// data.isSelected = button.isSelected
}
return cell
}