找到哪个单元格点击并执行搜寻
我已经使用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])
}
}
斯威夫特3
ERROR1: 您必须添加UITableViewDelegate
使用UITableView
delegate
方法。
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){}
这就是它!谢谢您的帮助! –
@SilviaCamara,不客气的朋友:) –
你将不得不顺应UITableViewDelegate
像你这样的UITableViewDataSource
,didSelectRowAtIndexPath
是UITableViewDelegate
方法
添加这些行
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
而且在viewDidLoad中
restuarantsTable.delegate = self
我认为你会犯错ð必须
转到设置委托在故事板以及该Tab和Ctrl +拖动到ViewController将其设置为代表
修改代码中的两件事情设置restuarantsTable.delegate = self
然后重新装入您的当您正在进行网络调用时,将在主线程上执行表格,这将在后台线程中处理。所以也请修改下面的代码。
DispatchQueue.main.async({
self.restuarantsTable.reloadData()
})
你将不得不遵守的UITableViewDelegate像你这样为UITableView DataSource,didSelectRowAtIndexPath是一个UITableViewDelegate方法 – 3stud1ant3
看到这里,https://*.com/questions/44938900/table-view-is-not-loading-data/44939308#44939308 –