2017-01-18 1 views
0

데이터가 여러 개인 TableView가 있고 각 셀에 3 개의 레이블이 있습니다.선택한 셀 (TableView)에서 데이터 가져 오기

어떻게 여기에 내가 실제로 변수 "제한"이 이후에 널 (null)이된다 또 다른 게시물에 요청했습니다 전체 코드 입니다

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) 데이터가 로컬에 저장하고, 만 limit이 전무 경우, 당신은 당신이 제대로 셀에 limitslimits[indexPath.row].limit를 할당 여부를 확인해야합니다. (그것이 지금이 순간 전무인가?) 나는 문제가에 생각 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 여기서 limit이 저장됩니다. var label1, var label2, var label3 :

가 사용자 지정 셀 전화 LimitCell이고 세 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 
} 
관련 문제