首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从选定单元格获取数据(TableView)

从选定单元格获取数据(TableView)
EN

Stack Overflow用户
提问于 2017-01-18 22:45:27
回答 2查看 734关注 0票数 0

我有一个包含多个数据单元格的TableView,每个单元格中有3个标签。

如何使用indexPath将所有3个label.text保存到另一个变量中

代码语言:javascript
复制
let indexPath = self.tableView.indexPathForSelectedRow

这是我在另一篇文章中问过的完整代码,在.observe之后,变量"limit“变成了null。所以我在想,如果我可以直接从单元格中获取数据。

代码语言:javascript
复制
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 =
            }




        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-01-19 00:45:45

你真的不想使用viewWithTag()。处理这种情况的最好方法是使用数据模型对象的属性将UITableViewCell子类化

代码语言:javascript
复制
class LimitCell: UITableViewCell {
   var limit: Limit {
       didSet {
           // configureCell()
       }
    }
}

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

代码语言:javascript
复制
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
    }
}
票数 0
EN

Stack Overflow用户

发布于 2017-01-19 00:51:36

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

1)如果数据来自服务器,您的代码不能保证在调用func preparevar limits已经获取了数据。

2)如果数据存储在本地,且只有limit为nil,则必须检查是否正确地将limits[indexPath.row].limit to limits赋值给该单元格(此时是否为nil?)我认为问题出在保存limitfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)中。

顺便说一句,实现func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)的更实用、更有效的方法是:

假设您的自定义单元格是call LimitCell,它有三个UILabels:var label1var label2var label3

代码语言:javascript
复制
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
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41722357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档