我使用 Google Places API 填充了带有餐厅名称的 UITableView
。现在,我正在尝试对不同的 View 执行 segue,同时知道使用 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.23001239999996&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])
}
}
最佳答案
您必须像对 UITableViewDataSource
那样遵守 UITableViewDelegate
,didSelectRowAtIndexPath
是一个 UITableViewDelegate
方法
添加这些行
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
并且在 viewDidLoad 中
restuarantsTable.delegate = self
我想你也必须在 Storyboard 中设置委托(delegate)
转到此选项卡并按住 ctrl 并拖动到 View Controller 以将其设置为委托(delegate)
关于ios - 查找哪个单元格被单击并执行 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45406695/