swift - didSelectRowAtIndexPath 遇到问题
问题描述
我现在正在弄乱桌子,我的目标是最终能够点击桌子并去某个地方。但是,我无法didSelectRowAtIndexPath
正常工作。该表正确显示,但当我选择它时没有任何反应:
class thirdController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
let theData: [String] = ["Dogs", "Cats", "Sheep", "Snakes"]
let cellReuseIdentifier = "cell"
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = CGRect(x: 0, y: 250, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-600)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return theData.count
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
cell.textLabel?.text = theData[indexPath.row]
return cell
}
// ERROR: This isn't working!
private func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
@IBAction func takeMeBack(_ sender: Any) {
let myAlert = UIAlertController(title: "Heading back home!", message: "Wanna go back?", preferredStyle: UIAlertController.Style.alert)
let nah = UIAlertAction(title: "I'll stay here!", style: UIAlertAction.Style.default, handler: nil)
let alright = UIAlertAction(title: "Get me out!", style: UIAlertAction.Style.default, handler: {_ in self.performSegue(withIdentifier: "goBackHome", sender: nil)})
myAlert.addAction(nah)
myAlert.addAction(alright)
present(myAlert, animated: true, completion: nil)
}
}
我正在使用 Swift 4,但我不确定出了什么问题。有任何想法吗?
解决方案
从函数中删除private
关键字。并阅读此
推荐阅读
- javascript - 使用 Socket.io 广播消息的问题
- java - 卡住 String.replace java 用正则表达式
- mongodb - 我的 mongodb 更新中的竞争条件?(使用流星)
- javascript - 由于 JS 导致的 iphone 键盘循环
- javascript - 如何为现有的 Javascript 库创建 Angular 库包装器?
- php - Magento 2 在开发过程中的 CPU 使用率应该有多高?
- python - 如何根据列和值范围创建多索引系列并检查它是否正确排序?
- java - TooLittleActualInvocations:
- microsoft-graph-api - Microsoft Graph API 状态代码 404 未找到错误
- php - 如何使用 Telegram Bot Api 通过机器人直接向任何用户(我有 user_id)发送消息