[英]Deselect row in tableview after programmatically selecting
I followed this thread first: How to programmatically select a row in UITableView in Swift 1.2 (Xcode 6.4) 我首先关注此线程: 如何在Swift 1.2(Xcode 6.4)中以编程方式在UITableView中选择一行
Which did help for the selection, but when I tap the screen to select another row, the previously (programmatically) set row will not trigger the deselect message. 这确实对选择有所帮助,但是当我点击屏幕以选择另一行时,先前(以编程方式)设置的行将不会触发取消选择消息。 The cells that are selected naturally (by tapping the screen), do receive the deselect message.
自然选择的单元格(通过点击屏幕)会收到取消选择消息。
I need that row that is programmatically selected to receive the deselect message (non-programmatically). 我需要以编程方式选择该行以接收取消选择消息(以非编程方式)。
(I'm working in a UITableView subclass - so it's not a delegate, it's overrides). (我正在UITableView子类中工作-因此它不是委托,而是重写)。
My code: 我的代码:
if let selectedRow = viewModel.selectedRow {
defaultSelectedPath = IndexPath(row: selectedRow, section: 0)
if let defaultSelectedPath = defaultSelectedPath {
let selectedCell = self.tableView.cellForRow(at: defaultSelectedPath)
self.tableView.selectRow(at: defaultSelectedPath, animated: true, scrollPosition: .none)
self.tableView(self.tableView, didSelectRowAt: defaultSelectedPath)
selectedCell?.accessoryType = .checkmark
}
}
Hack Fix: 黑客修复:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
if let defaultSelectedPath = self.defaultSelectedPath {
if indexPath != defaultSelectedPath {
cell = tableView.cellForRow(at: defaultSelectedPath)
tableView.deselectRow(at: defaultSelectedPath, animated: false)
self.tableView(self.tableView, didDeselectRowAt: defaultSelectedPath)
cell?.accessoryType = .none
}
self.defaultSelectedPath = nil
}
}
Please tell me there's a better way to do this. 请告诉我,有一种更好的方法可以做到这一点。
you can use :- 您可以使用 :-
self.clearsSelectionOnViewWillAppear = true
on viewDidLoad()
在
viewDidLoad()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.