简体   繁体   中英

UITableView select row doesn't work

Everything works, except when I click on a row.. nothing happens it should output 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)!")
    }

}

First, you need to inherit from UITableViewDelegate (class ... : UIViewController, UITableViewDelegate.. ).

Then, you need to assign

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

tentatively in your viewDidLoad.

Edit: As @zsteed mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM