2016-12-31 3 views
0

ちょっと、私のデータベースのレストランをリストするこのテーブルビューコントローラがあります。私がしたいのは、セル/行が選択されていれば、レストランに関する詳細ページが開きます。何らかの理由で、私は、インデックスを取得することはできませんか、そのは、FUNCのにtableViewにつもりはない(のtableView:のUITableView、didSelectRowAtIndexPath indexPath:NSIndexPath):テーブルビューセルからセルインデックスを取得

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) 
    } 
} 
} 
+0

あなたはすでに 'didSelectRowAtIndexPath'にresNameを取得しています...だから' vc.resNam = resName'を使用してください。 –

答えて

0

私はセグエせずに、このようにそれをやっている:

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

ありがとうございました! –

+0

ご承知おきください、将来的に誰かを助けるかもしれません:)喜んで役立った – JuicyFruit

0

あなたがこれを使用する必要が私にここにインデックスを与えるためには、私が持っているものですインデックスを取得する関数

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{   
      print(indexPath.row)   
} 

インデックスを取得します。

0

をすべてのコードの第3そうdidSelectRowAtの署名が間違っている、明らかスウィフトです。

別の解決策は、sender

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("Row \(indexPath.row)selected") 
    performSegue(withIdentifier: "RestaurantDetailView", sender: indexPath) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "RestaurantDetailView" { 
     let indexPath = sender as! IndexPath 
     let restaurant = restaurantArray[indexPath.row] 
     let resName = restaurant.name 
     print(resName!) 
     let vc = segue.destination as! RestaurantDetailViewController 
     vc.resNam = resName 
    } 
} 

または静止テーブルビューセルにセグエを接続しやすいようindexpathを通過させることです。 prepare(forが直接呼び出され、セルがsenderパラメータとして渡されるため、didSelectRow...を削除できます。

PS:なぜnameはオプションですか?実際の生活では名前のないレストランについて聞いたことがありますか?

関連する問題