Ios 查找单击的单元格并执行Segue
我已经使用Google Places API在UITableView中填充了餐厅名称。现在,我试图执行一个到另一个视图的序列,同时知道哪个单元格是用IndexPath单击的。我尝试过使用didSelectAtIndexRowPath函数,但它无法识别点击/点击。它应该将数字打印到控制台。我做错了什么Ios 查找单击的单元格并执行Segue,ios,objective-c,iphone,swift,uitableview,Ios,Objective C,Iphone,Swift,Uitableview,我已经使用Google Places API在UITableView中填充了餐厅名称。现在,我试图执行一个到另一个视图的序列,同时知道哪个单元格是用IndexPath单击的。我尝试过使用didSelectAtIndexRowPath函数,但它无法识别点击/点击。它应该将数字打印到控制台。我做错了什么 import UIKit class ViewController: UIViewController, UITableViewDataSource { var myIndex = 0
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
我想你们也应该在故事板上设置代理
转到此选项卡,按住ctrl键并拖动到viewcontroller以将其设置为委派修改代码集中的两个内容resturantstable.delegate=self,然后在进行网络调用时在主线程上重新加载表,该调用将在后台线程中处理。所以也修改下面的代码
DispatchQueue.main.async({
self.restuarantsTable.reloadData()
})
斯威夫特3
错误1:
要使用UITableView委托方法,必须添加UITableViewDelegate协议
在viewDidLoad中:
错误2:
协议必须与公众确认。你申报为私人
改变
private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){}
到
或低于swift 3
您必须像对待UITableViewDataSource一样遵守UITableViewDelegate,didSelectRowAtIndexPath是一种UITableViewDelegate方法。在这里,如何将其设置为委派?谢谢你的快速回复!没有调用与您对DataSourcedDedElectroWMethod所做的相同的方法?这与DidSelectRowatineXpath不同吗?对不起,是Swift的新成员。不,是相同的方法,是否调用了此方法?就是这样!谢谢你的帮助!
override func viewDidLoad() {
super.viewDidLoad()
restuarantsTable.dataSource = self
restuarantsTable.delegate = self
}
private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){}