2017-07-31 25 views
0

我已经使用Google Places API填充了餐馆名称UITableView。现在,我正在尝试在知道使用IndexPath单击了哪个单元格的同时执行了一个不同的视图。我试过使用didSelectAtIndexRowPath功能,但它不能识别点击/点击。它应该打印出数字到控制台。我究竟做错了什么?找到哪个单元格点击并执行搜寻

import UIKit 

class ViewController: UIViewController, UITableViewDataSource { 

    var myIndex = 0 
    @IBOutlet var restuarantsTable: UITableView! 
    var restaurantsList = [NSDictionary]() 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     restuarantsTable.dataSource = self 

     let url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=26.2034071,-98.230&radius=500&type=restaurant&keyword=tacos&key=AIzaSyBRQZV4SOpcJ-icCe9BuZlI48MzONwH5Dw" 
     downloadRestaurants(urlString: url) { (array) ->() in 
      self.restaurantsList = array as! [NSDictionary] 
      // print(self.restaurantsList.count) 
      self.restuarantsTable.reloadData() 
     } 

    } 

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return restaurantsList.count 
    } 


    @available(iOS 2.0, *) 
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let RCell = tableView.dequeueReusableCell(withIdentifier: "RCell", for: indexPath) 
     // let imgURL = NSURL(string: imgURLArray[indexPath.row]) 
     // let imageView = RCell.viewWithTag(350) as! UIImageView 

     RCell.textLabel!.text = restaurantsList[indexPath.row]["name"] as? String 
     // RCell.imageView?.image = restaurantsList[indexPath.row]["photos"] as? UIImage 

     return RCell 
    } 

    private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { 
     self.performSegue(withIdentifier: "detailSegue", sender: indexPath) 

     print(restaurantsList[indexPath.row]) 
    } 

} 
+0

你将不得不遵守的UITableViewDelegate像你这样为UITableView DataSource,didSelectRowAtIndexPath是一个UITableViewDelegate方法 – 3stud1ant3

+0

看到这里,https://stackoverflow.com/questions/44938900/table-view-is-not-loading-data/44939308#44939308 –

回答

0

斯威夫特3

ERROR1: 您必须添加UITableViewDelegate 使用UITableViewdelegate方法。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{} 

viewDidLoad()

override func viewDidLoad() { 
    super.viewDidLoad() 

    restuarantsTable.dataSource = self 
    restuarantsTable.delegate = self 
} 

错误2: 的必须与公众确认。您声明为private

变化

private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {} 

或(以下SWIFT 3)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){} 
+0

这就是它!谢谢您的帮助! –

+0

@SilviaCamara,不客气的朋友:) –

1

你将不得不顺应UITableViewDelegate像你这样的UITableViewDataSourcedidSelectRowAtIndexPathUITableViewDelegate方法

添加这些行

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

而且在viewDidLoad中

restuarantsTable.delegate = self 

我认为你会犯错ð必须

enter image description here

转到设置委托在故事板以及该Tab和Ctrl +拖动到ViewController将其设置为代表

+0

我该如何去设置它委托?感谢您的快速响应顺便说一句! –

+0

与数据源相同的方式 – 3stud1ant3

+0

好了,做到了,但仍然没有 –

0

修改代码中的两件事情设置restuarantsTable.delegate = self然后重新装入您的当您正在进行网络调用时,将在主线程上执行表格,这将在后台线程中处理。所以也请修改下面的代码。

DispatchQueue.main.async({ 
self.restuarantsTable.reloadData() 
})