2017-01-26 3 views
0

この問題が発生しています。 Firebaseからjsonファイルをダウンロードして、TableViewに正常に表示しています。そのTableViewには、2つのラベルを持つカスタムセルがあります。カスタムセルからDetail ViewControllerへのセグのリンクを設定しました。それはうまく動作しますが、今ではセルラベルのテキストコンテンツを取得し、宛先のViewControllerに送信したいと考えています。宛先の表示にcustomCellラベルテキストを送信

私はこれまでこれをやっていませんでしたが、カスタムセルのlabel.textからこれを実装するのに問題があります。

私はラベルタグを使用しています(label1とlabel2、私はラベル名をいつか変更するかもしれませんが)。

質問は、選択した行の2つのラベルからテキストの内容を取得し、それらを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) 

最終セグエ

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

関連する問題