我是Swift的初学者,可能这是基础知识,但是我需要获取在tableview中单击的行(单元格)的索引。
我的TableViewController具有预定义的方法,但是找不到我想要的东西。
行(单元格)是字符串数组中的字符串。
答案 0 :(得分:3)
执行表视图委托,则可以使用方法tableView(_:didSelectRowAt:)
:https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614877-tableview
具体地说:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("your row number: \(indexPath.row)")
}
答案 1 :(得分:0)
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
答案 2 :(得分:-1)
您的TableView包含TableViewCells。触摸单元格时的所有逻辑都可以在其中编程
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
更新
indexPath.row
将为您提供所选tableviewcell的索引