데이터가 여러 개인 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 =
}
}
}
}
지금까지 작성한 코드 표시 – mlidal
어떻게 그 셀에 정보를 넣었습니까? 'tableView (_tableView : cellForRowAt :)'의 코드는 무엇입니까? 배열 (dataSource)에서 텍스트를 설정 했습니까? 그런 다음 indexPath를 검색 했으므로 배열 (dataSource)을 리드했습니다. – Larme
@Larme firebase에서 데이터를 가져옵니다. 그래서 생각보다 복잡해 보입니다. – jlassap