UITableView选择行不起作用

时间:2017-01-19 14:22:22

标签: ios swift uitableview

一切正常,除非我点击一行......没有任何反应应该输出You selected cell number:

class JokesController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var jokes_list: UITableView!
    var CountCells = 0
    var CellsData = [[String: Any]]()

    override func viewDidLoad() {

        super.viewDidLoad();

        Alamofire.request("http://localhost:8080/jokes.php").responseJSON{ response in

            if let JSON = response.result.value as? [[String: Any]] {
                self.CellsData = JSON
                self.CountCells = JSON.count
                self.jokes_list.reloadData()
            }else{
                debugPrint("failed")
            }
        }



        // Do any additional setup after loading the view, typically from a nib.
    }


    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 CellsData.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell

        cell.textLabel?.text = CellsData[indexPath.row]["title"] as! String?

        return cell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        debugPrint("You selected cell number: \(indexPath.row)!")
    }

}

1 个答案:

答案 0 :(得分:2)

首先,您需要继承UITableViewDelegate(类...:UIViewController,UITableViewDelegate ..)。

然后,您需要指定

self.jokes_list.delegate = self
self.jokes_list.dataSource = self

暂时在你的viewDidLoad中。

编辑:正如@zsteed所说。