从tableviewcell获取单元格索引 [英] Get cell index from tableviewcell

查看:84
本文介绍了从tableviewcell获取单元格索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有这个tableview控制器,它从我的数据库中列出了餐馆.我想做的是,如果选择了单元格/行,则会打开有关餐厅的更多详细信息页面.出于某种原因,我无法检索索引,或者无法访问func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath)给我索引,这就是我所拥有的:

Hey I have this tableview controller that list restaurants from my database. what I want to do is if the cell/row is selected it opens a more detail page about the restaurant. for some reason I can't retrieve the index or its not going to the func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) to give me index here is what I have:

class HomeTableViewController: UITableViewController{


var restaurantArray = [Restaurant]()
var resTransfer: Restaurant?
var resName: String?



var dataBaseRef: FIRDatabaseReference! {
    return FIRDatabase.database().reference()
}


override func viewDidLoad() {
    super.viewDidLoad()
    title = "Home"
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Main Menu", style: .plain, target: self, action: #selector(SSASideMenu.presentLeftMenuViewController))
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    fetchRestaurants()
}

func fetchRestaurants(){
    FIRDatabase.database().reference().child("AthensRestaurants/Restaurants").observe(.value, with: { (snapshot) in
        var results = [Restaurant]()

        for res in snapshot.children{
            let res = Restaurant(snapshot: res as! FIRDataSnapshot)
            results.append(res)
        }

        self.restaurantArray = results.sorted(by: { (u1, u2) -> Bool in
            u1.name < u2.name
        })
        self.tableView.reloadData()

}) { (error) in
print(error.localizedDescription)
}
}

// MARK: - Table view data source
// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return restaurantArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantsCell", for: indexPath) as! RestaurantsTableViewCell

    // Configure the cell...

    cell.configureCell(res: restaurantArray[indexPath.row])

    return cell
}




func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("Row \(indexPath.row)selected")
    resTransfer = restaurantArray[indexPath.row]
    resName = restaurantArray[indexPath.row].name
    print(resName as Any)
    performSegue(withIdentifier: "RestaurantDetailView", sender: self)
   }







override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if(segue.identifier == "RestaurantDetailView") {
      let vc = segue.destination as! RestaurantDetailViewController
       vc.resNam = restaurantArray.first?.name //instead of first should be index of cell! 
        print(vc.resNam! as String)
    }
}   
}

推荐答案

我是这样做的,没有秘密:

I am doing it like this, without segue:

func showRestaurantViewControllerWith(_ id: String) {
    let storyBoard = UIStoryboard(name: "RestaurantCard", bundle: nil)
    let destinationVC = storyBoard.instantiateViewController(withIdentifier: "RestaurantCard") as! RestaurantCardViewController
    destinationVC.restaurant.id = id
    self.navigationController?.pushViewController(destinationVC, animated: true)
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.showRestaurantViewControllerWith(self.allRestaurants[indexPath.row].id)
}

这篇关于从tableviewcell获取单元格索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆