【问题标题】:Finding Which Cell Was Clicked and Performing Segue查找单击了哪个单元格并执行 Segue
【发布时间】:2017-07-31 02:30:49
【问题描述】:

我使用 Google Places API 填充了带有餐厅名称的 UITableView。现在,我试图在知道使用 IndexPath 单击了哪个单元格的同时,对不同的视图执行 segue。我试过使用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])
    }

}

【问题讨论】:

标签: ios objective-c iphone swift uitableview


【解决方案1】:

你必须遵守 UITableViewDelegate 就像你对 UITableViewDataSource 所做的那样,didSelectRowAtIndexPath 是一个 UITableViewDelegate 方法

添加这些行

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

在 viewDidLoad 中

restuarantsTable.delegate = self

我认为您还必须在情节提要中设置委托

转到此选项卡并 ctrl+拖动到视图控制器以将其设置为委托

【讨论】:

  • 如何将其设置为委托?顺便说一句,感谢您的快速响应!
  • 和你对 dataSource 做的一样
  • didSelectRowMethod 没有被调用?
  • 这与 didSelectRowAtIndexPath 有什么不同?抱歉,Swift 新手。
  • 不是同一个方法,这个方法被调用了吗
【解决方案2】:

斯威夫特 3

错误1: 您必须添加 UITableViewDelegate protocol 才能使用 UITableView delegate 方法。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{}

viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()

    restuarantsTable.dataSource = self
    restuarantsTable.delegate = self
}

错误 2: protocol 必须与公众确认。你声明为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){}

【讨论】:

  • 就是这样!感谢您的帮助!
【解决方案3】:

修改代码集restuarantsTable.delegate = self 中的两件事,然后在进行网络调用时在主线程上重新加载表,这将在后台线程中处理。所以还要修改下面的代码。

DispatchQueue.main.async({
 self.restuarantsTable.reloadData()
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多