2017-01-26 1 views
0

이 문제가 발생합니다. Firebase에서 json 파일을 다운로드하고 TableView에 성공적으로 표시하고 있습니다. 해당 TableView에는 두 개의 레이블이있는 사용자 정의 셀이 있습니다. 사용자 정의 셀에서 Detail ViewController로 segue 링크를 설정했습니다. 그 잘 작동하지만 지금은 셀 레이블 텍스트 콘텐츠를 받아 대상 ViewController 보내고 싶습니다.대상보기에 customCell 레이블 텍스트 전송

나는 과거에는이 작업을 수행하는 데 문제가 없었지만 사용자 정의 셀의 label.text에서이를 구현하는 데 문제가 있습니다.

나는 라벨 태그 (label1과 label2를 사용하고 있는데, 언젠가는 라벨 이름을 변경할 수 있음).

질문은 선택한 행의 두 레이블에서 텍스트 내용을 가져 와서 DetailViewController에 전달하는 방법입니다. 지금까지 내 모든 시도는 실패했습니다. 여기

valueToPass = currentCell?.textLabel?.text 

코드입니다 : 환영

struct postStruct { 
    let property : String! 
    let propType : String! 
    } 

class TableViewController: UITableViewController { 

var posts = [postStruct]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.delegate = self 
    self.tableView.dataSource = self 

    let databaseRef = FIRDatabase.database().reference() 

    databaseRef.child("users").queryOrderedByKey().observe(.childAdded, with: { 
     snapshot in 

     let snapshotValue = snapshot.value as? NSDictionary 
     let property = snapshotValue!["property"] as? String 

     let propType = snapshotValue!["type"] as? String 

     self.posts.insert(postStruct(property: property, propType: propType), at: 0) 
     self.tableView.reloadData() 
    }) 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return posts.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 

    let label1 = cell?.viewWithTag(1) as! UILabel 
    label1.text = posts[indexPath.row].property 

    let label2 = cell?.viewWithTag(2) as! UILabel 
    label2.text = posts[indexPath.row].propType 
    // print(posts) 
    // cell.setcell(valueToPass.label1) 
     return cell! 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

var valueToPass:String! 

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 

    // Get Cell Label 
    let indexPath = tableView.indexPathForSelectedRow; 
    let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!; 

    valueToPass = currentCell?.textLabel?.text 
    performSegue(withIdentifier: "detailSegue", sender: self) 
     } 

어떤 도움, 특정 코드 예제에서 나는에 일을해야 뭔가가 있나요. 예상에 많은 감사를드립니다

답변

1

TableCell에 대한 맞춤 클래스를 만들고 StoryBoard에 아울렛을 연결하십시오.

cellForRowAt의 세포 생성

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") As! MyCustomCell 
cell.label1.text = posts[indexPath.row].property 
cell.label2.text = posts[indexPath.row].propType 

된다 다음 didSelectRowAtIndexPath는 대한

let indexPath = tableView.indexPathForSelectedRow 
let currentCell = tableView.cellForRow(at: indexPath!) as! MyCustomCell 

self.valueToPass = currentCell.label1?.text 
performSegue(withIdentifier: "detailSegue", sender: self) 

마지막 SEGUE

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
{ 
    if segue.identifier == "detailSegue" // you may have more than one segue, so you should check 
    { 
     let destinationController : DestinationViewControllerClass = segue.destination as! DestinationViewControllerClass 
     destinationController.valueToPass = self.valueToPass 
    } 
} 
+0

안녕을 준비하는 경우에 당신이 필요로하는 것, 감사가된다 대응할 시간을내어 그러나 몇 가지 문제가 있습니다. 커스텀 클래스는 UITableViewController의 하위 클래스이거나 TableViewCell의 하위 클래스 여야 하는가? TableViewCell에서 콘센트를 연결할 수 없습니다. TableViewController에 있어야합니까? 내가 제공 한 코드가 TableViewController에 있다고 생각하면 맞습니까? 그렇다면 예를 들어 시도 할 때 약간의 오류가 발생했습니다. let cell = tableView.dequeueReusableCell (withIdentifier : "Cell") As! MyCustomCell은 '한 줄에 연속 된 선언은 다음과 같이 구분해야합니다. ' – PhilipS

+0

아마 가장 좋은 대화방에 가져 가면 - 내가 TableView 전화 하나를 설정했습니다 ... – Russell

+0

안녕하세요, 당신이 다른 문제에 나를 도울 수 있는지 궁금해. 나는 질문을했지만 대답을받지 못했다. 실제로 당신이 친절하게 저를 도왔던 마지막 문제에서 계속됩니다 : http://stackoverflow.com/questions/41937700/swift-3-passing-var-through-a-series-of-segues-viewcontrollers – PhilipS