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

标签 ios objective-c iphone swift uitableview

我使用 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/

相关文章:

ios - UISlider 跟踪标注未与拇指对齐

iphone - 当 UITableView 滚动时如何取消选择 UITableViewCell?

ios - 如何从 PARSE.com 中的 PFquery 获取 NSArray 的 objectId

ios - 如何将值从 UIView 的子类传递到 UIViewController 类

iphone - UIButton 颜色问题

ios - 我怎样才能使应用程序图标像 iOS 内置的时钟和日历图标一样响应

iphone - 我可以在不手动转换所有顶点的情况下进行 openGL 几何批处理吗?

iphone - 在 iOS 中动态更改启动图像

objective-c - UIAlertView 在 vi​​ewWillDisappear 中

javascript - 在 iOS 模块中访问 Appcelerator 属性