我已經使用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])
}
}
你將不得不遵守的UITableViewDelegate像你這樣爲UITableView DataSource,didSelectRowAtIndexPath是一個UITableViewDelegate方法 – 3stud1ant3
看到這裏,https://stackoverflow.com/questions/44938900/table-view-is-not-loading-data/44939308#44939308 –