我有一个包含多个数据单元格的TableView,每个单元格中有3个标签。
如何使用indexPath将所有3个label.text保存到另一个变量中
let indexPath = self.tableView.indexPathForSelectedRow这是我在另一篇文章中问过的完整代码,在.observe之后,变量"limit“变成了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 =
}
}
}
}发布于 2017-01-19 00:45:45
你真的不想使用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
}
}发布于 2017-01-19 00:51:36
您似乎正在使用回调来获取数据。数据是来自服务器还是存储在本地?
1)如果数据来自服务器,您的代码不能保证在调用func prepare时var limits已经获取了数据。
2)如果数据存储在本地,且只有limit为nil,则必须检查是否正确地将limits[indexPath.row].limit to limits赋值给该单元格(此时是否为nil?)我认为问题出在保存limit的func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)中。
顺便说一句,实现func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)的更实用、更有效的方法是:
假设您的自定义单元格是call LimitCell,它有三个UILabels:var label1、var label2和var 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
}https://stackoverflow.com/questions/41722357
复制相似问题