尝试调用要以编程方式选择的 TableView 行。
我的所有委托(delegate)均已正确设置,并且 UITableView
已链接到 IB 中。所有其他 UITableView
方法都可以正常工作,并且位于主 ViewController 或其扩展中。当调用以下内容时,我得到Thread 1: Fatal error: Unexpectedlyfound nil while unwrapping anOptional value
。
func selectTableViewRowProgrammatically () {
let indexPath = IndexPath(row: someVariable, section: 0)
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableView.ScrollPosition.none)
tableView.delegate?.tableView!(self.tableView, didSelectRowAt: indexPath)
tableView.reloadData()
}
self.tableView.selectRow(at:indexPath,animated:false,scrollPosition:UITableView.ScrollPosition.none)上显示错误
谢谢!
最佳答案
你可以尝试下面的代码吗
let indexPath = IndexPath(row: someVariable, section: 0)
UIView.animate(withDuration: 0.1, animations: {
tableView.scrollToRow(at: indexPath, at: .top, animated: false)
}) { (true) in
tableView.delegate?.tableView!(self.tableView, didSelectRowAt: indexPath)
}
关于ios - 以编程方式设置 UITableView 行选择返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54357229/