我的tableView单元格中有图像,所以我将单元格选择样式设置为无,以避免单元格在选中时突出显示。
我现在正试图在tableView中实现编辑,允许用户选择多个单元格,填写单元格左侧的复选标记。但是,这似乎不适用于None的selectionStyle - 圆圈仍未填充。
有什么方法可以解决这个问题吗?
感谢?
答案 0 :(得分:0)
如果你不能直接选择单元格,你可以在单元格中放置一个uibutton并识别在哪个单元格中按下了被按下的按钮。为此,你可以将button标签设置为等于cellForRowAtIndexPath中的indexPath.row方法
以下代码打印正确的行号。希望这会有所帮助。
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var onButton: UIButton!
}
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func buttonClicked(sender:UIButton) {
let buttonRow = sender.tag
print(buttonRow)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyTableViewCell
cell.onButton.tag = indexPath.row
cell.onButton.addTarget(self, action: #selector(ViewController.buttonClicked(sender:)), for: .touchUpInside)
return cell
}}