2017-07-31 27 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() 
})