2017-01-18 10 views
0

私はいくつかのセルのセルを持つTableViewを持っており、各セルに3つのラベルがあります。選択したセル(TableView)からデータを取得

は、どのように私はここで私は実際に変数「リミット」は、後にゼロになることを別のポストに求めてきました完全なコード ある

let indexPath = self.tableView.indexPathForSelectedRow 

indexPath

で別の変数にすべての3 label.textを保存することができます。事を観察する。 セルから直接データを取得できるかどうかを考えています。

import UIKit 
import Firebase 
import FirebaseDatabase 

struct limitStruct{ 
    var name : String! 
    var today : String! 
    var limit : String! 

} 

class CalcViewController: UITableViewController { 

    var limits = [limitStruct]() 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     navigationController?.navigationBar.barTintColor = UIColor.black 
     self.title = "Calculation" 
     navigationController!.navigationBar.titleTextAttributes = 
      [NSForegroundColorAttributeName: UIColor.white] 
     let databaseReference = FIRDatabase.database().reference() 

     databaseReference.child("Limit").queryOrderedByKey().observe(.childAdded, with: { 
      snapshot in 
      var snapshotValue = snapshot.value as? NSDictionary 

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

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

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


      self.limits.insert(limitStruct(name:name, today:today, limit: limit), at: self.limits.count) 
      self.tableView.reloadData() 
     }) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

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

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

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

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

     let label2 = cell?.viewWithTag(2) as! UILabel 
     label2.text = limits[indexPath.row].today 

     let label3 = cell?.viewWithTag(3) as! UILabel 
     label3.text = limits[indexPath.row].limit 

     return cell! 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "showDetails"{ 

      let svc = segue.destination as! CirSliderViewController; 

      if let indexPath = self.tableView.indexPathForSelectedRow{ 
//    svc.RsegueData = 
      } 




     } 
    } 
} 
+0

これまでに書いたコードを表示してください。 – mlidal

+0

どのように情報をそのセルに入れましたか? 'tableView(_tableView:cellForRowAt:)'のコードは何ですか?配列(dataSource)からテキストを設定しましたか?その後、indexPathを取得するので、配列(dataSource)をリードしただけです。 – Larme

+0

@Larme私はfirebaseからデータを取得しています。それは思ったよりも複雑に思えます。 – jlassap

答えて

0

あなたは本当にviewWithTag()を使用したくありません。

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Limit", forIndex: indexPath) as! LimitCell 
    cell.limit = limits[indexPath.row] 
    return cell 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let svc = segue.destination as? CirSliderViewController, cell = sender as? LimitCell { 
     svc.RsegueData = cell.limit 
    } 
} 
-1

あなたが得るためにコールバックを使用しているようです。これを処理するための最良の方法は、あなたのビューコントローラに

class LimitCell: UITableViewCell { 
    var limit: Limit { 
     didSet { 
      // configureCell() 
     } 
    } 
} 

その後、あなたのデータモデルオブジェクトのプロパティで、UITableViewCellをサブクラス化することですデータ。データはサーバーから取得されるか、ローカルに保存されますか?

1)サーバーからのデータの場合は、func prepareが呼び出されたときにvar limitsが既にデータを取得していることを保証できません。

2)データがローカルに格納されている、とONLY limitがnilであれば、あなたは正しくセルにlimitslimits[indexPath.row].limitを割り当てるかどうかを確認する必要があります。(それは現時点ではnilで?)私はこの問題はであると思いますfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)limitが保存されます。 var label1var label2var label3

カスタムセルがコールLimitCellであり、3つのUILabelsを持って言うことができます:ところで

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)を実装するために、より実用的かつ効率的な方法があることです。

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Limit") as! LimitCell 

    cell.label1.text = limits[indexPath.row].name 
    cell.label2.text = limits[indexPath.row].today 
    cell.label3.text = limits[indexPath.row].limit 

    return cell 
} 
関連する問題