UITableView didSelectRow 返回错误的行索引值 [英] UITableView didSelectRow returns wrong row index value
本文介绍了UITableView didSelectRow 返回错误的行索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我选择一行时,它返回一个更大或更小的行索引值,但永远不会是正确的值.如上图我选择了 14 返回 16.
When I select a row it returns a row index value bigger or smaller but never the correct value. As on the picture I selected 14 returned 16.
这是我使用的代码:
@IBOutlet var cheatTable: UITableView!
let cheetSheet = ["one", "two", "three", "four", "five"]
override func viewDidLoad() {
super.viewDidLoad()
cheatTable.delegate = self
cheatTable.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseID")!
let text:String = "\(indexPath.row)"
cell.textLabel?.text = text
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
guard let currentCell = tableView.cellForRow(at: indexPath) else {
return
}
let alertController = UIAlertController(title: "Hint", message: "You have selected row \(indexPath.row).", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
}
我该怎么做才能获得正确的行索引?谢谢!
What could I do to get the correct row index? Thanks!
推荐答案
检查你的委托名称不是 didDeselectRowAt
它是 didSelectRowAt
check your delegate name is not didDeselectRowAt
it is didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let alertController = UIAlertController(title: "Hint", message: "You have selected row \(indexPath.row).", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
}
按照你的代码,我创建了示例项目
as per your code follow I created the sample project
这篇关于UITableView didSelectRow 返回错误的行索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文