2017-05-16 2 views
1

어떤 tableviewcell을 선택했는지 알기 위해 그 셀의 레이블 값을 가져 와서 다음보기 컨트롤러로 전달하려고합니다. 내 셀의 레이블 값은 string이고 숫자 값은 Int입니다. firebase 데이터베이스를 사용하여 모든 데이터를 가져옵니다.어떤 tableviewcell이 선택되었는지 확인하고 다른 viewcontroller에 값을 전달하십시오.

내 코드 :

import UIKit 


class PlacesTableViewController: UITableViewController { 
//MARK: Properties 
    @IBOutlet weak var placesTableView: UITableView! 
var places = [Places]() 
override func viewDidLoad() 
    { 
     super.viewDidLoad() 




     // Loads data to cell. 
     loadData() 
    } 
override func numberOfSections(in tableView: UITableView) -> Int 
    { 
     return 1 
    } 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     //return the number of rows 
     return places.count 
    } 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     // Table view cells are reused and should be dequeued using a cell identifier. 
     let cellIdentifier = "PlacesTableViewCell" 

     guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell else { 
      fatalError("The dequeued cell is not an instance of PlacesTableView Cell.") 
     } 

     let place = places[indexPath.row] 

     cell.placeLabel.text = place.name 
     cell.ratingControl.rating = place.rating 

     return cell 

    } 
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
     print(places[indexPath.section]) 
     self.performSegue(withIdentifier: "ShowCommentsTableViewController", sender: nil) 
    } 
} 

답변

0

이 시도 :

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
     let selectedPlace = places[indexPath.section] 
     self.performSegue(withIdentifier: "ShowCommentsTableViewController", sender: selectedPlace) 
    } 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if let selectedPlace = sender as? Places, 
     let destViewController = segue.destination as? SecondViewController { 
     destViewController.place = selectedPlace 
    } 
} 
1

당신이 당신은 대상 뷰 컨트롤러와 저장소에 변수를 만들 필요가 send-data-from-tableview-to-detailview-swift

유용이 스레드를 따를 수 있습니다 목적지보기 컨트롤러로 이동하기 전에 해당 변수의 데이터. 이 링크는 도움이 될 것입니다.

+0

내가 스레드에 대해 감사해야 할 것을 이해합니다. 하지만 prepareForSegue func를 재정의하려고 할 때 문제가 있습니다. –

+0

stackoverflow에서 질문을 게시하고 주시기 바랍니다. :) –

+0

나는 그것을 발견했다. Swift 3 업데이트 코드였습니다. 'override func prepare (segue : UIStoryboardSegue, 발신자 : Any?)'대신에. –

관련 문제