2017-01-19 3 views
0

Все работает, за исключением того, когда я нажимаю на строке .. ничего не происходит, он должен вывести You selected cell number:UITableView выберите строку не работает

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

Возможный дубликат [didSelectRowAtIndexPath не работает, Swift 3] (http://stackoverflow.com/questions/40491818/didselectrowatindexpath-not-working-swift-3) Другими словами: смешивание Swift 2.x и Swift 3 while подпись метода изменилась. – Larme

+0

Установили ли вы DataSource/делегат TableView? – zsteed

ответ

2

Во-первых, вы должны наследовать от UITableViewDelegate (класс ...: UIViewController , UITableViewDelegate ..).

Затем вам нужно назначить

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

ориентировочно в вашем viewDidLoad.

Редактировать: Как упоминалось в @zsteed.

+0

Ты мужчина! Спасибо! – Uffo

+0

Ницца, приветствует человека! –

Смежные вопросы