gpt4 book ai didi

ios - 查找哪个单元格被单击并执行 Segue

转载 作者:行者123 更新时间:2023-11-28 15:24:54 24 4
gpt4 key购买 nike

我使用 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 那样遵守 UITableViewDelegatedidSelectRowAtIndexPath 是一个 UITableViewDelegate 方法

添加这些行

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

并且在 viewDidLoad 中

restuarantsTable.delegate = self

我想你也必须在 Storyboard 中设置委托(delegate)

enter image description here

转到此选项卡并按住 ctrl 并拖动到 View Controller 以将其设置为委托(delegate)

关于ios - 查找哪个单元格被单击并执行 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45406695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com