从所选单元格中获取数据(TableView)

时间:2017-01-18 14:45:27

标签: swift swift3 tableview

我有一个带有几个数据单元的TableView,每个单元格中有3个标签。

如何使用indexPath

将所有3个label.text保存到另一个变量中
let indexPath = self.tableView.indexPathForSelectedRow

这是完整的代码 我实际上在另一篇文章中问过变量"限制"在.observe之后变为null。 所以我想我是否可以直接从细胞中获取数据。

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 =
            }




        }
    }
}

2 个答案:

答案 0 :(得分:0)

您真的不想使用viewWithTag()。处理此问题的最佳方法是使用数据模型对象的属性

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

然后在你的视图控制器中:

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 :(得分:-1)

您似乎正在使用回调来获取数据。数据是来自服务器还是存储在本地?

1)如果数据来自服务器,则代码无法保证var limits在调用func prepare时已获得数据。

2)如果数据存储在本地,并且只有limit为零,则必须检查是否正确地将limits[indexPath.row].limit分配给limits到单元格。(是否为零)在这一刻?)我认为问题出在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath),其中limit已保存。

顺便说一下,实施func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)的更实际有效的方法是:

假设您的自定义单元格为LimitCell,并且有三个UIL用户:var label1var label2var label3

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
}
相关问题