我创建了UITableView,其中包含1个部分和3行。每行中的文本都是硬编码的,我需要通过点击每一行来调用特定的操作。 为什么UITableView delegae方法func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)未在以下代码中调用?
@IBOutlet weak var tbvSetting: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tbvSetting.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
switch indexPath.row {
case 0:
cell.textLabel?.text = "Countries"
case 1:
cell.textLabel?.text = "Managers"
case 2:
cell.textLabel?.text = "Customers"
default:
break
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
感谢提示。
答案 0 :(得分:1)
将self
设置为delegate
的{{1}}
您可以在Storyboard(您似乎正在使用)或代码中执行此操作:
tableView
因为,这还没有设置,即使tableView单元格正在override func viewDidLoad() {
super.viewDidLoad()
tbvSetting.delegate = self
tbvSetting.reloadData()
}
,操作系统也不知道谁是选择手势的处理程序。