遇到didSelectRowAtIndexPath的问题

时间:2019-03-02 06:08:42

标签: swift

此刻我正在弄乱桌子,我的目标是最终能够单击桌子并在某个地方进行搜索。但是,我无法使用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,但不确定出现什么问题。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

从函数中删除private关键字。 并阅读this