xcode 从 Swift 中的表格视图单元格访问数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25825216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:33:00  来源:igfitidea点击:

Access data from table view cell in Swift

xcodeuitableviewswift

提问by yaboi

I'm having trouble accessing an optional string in swift from a table view cell. Each cell has a title label as well as a detail text label and the amount of cells at any given time in the table view is dependent on the user (the table view is used to display saved values). Since the strings saved as the detail text label's text in each cell are way too long to read in the table view, I wish to load them in another view that is segued to when a cell is pressed. This is my prepare for segue method:

我在从表视图单元格快速访问可选字符串时遇到问题。每个单元格都有一个标题标签和一个详细文本标签,表格视图中任何给定时间的单元格数量取决于用户(表格视图用于显示保存的值)。由于在每个单元格中保存为详细文本标签文本的字符串太长而无法在表格视图中阅读,我希望将它们加载到另一个视图中,该视图在单元格被按下时进行。这是我准备转场的方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


        if segue.identifier == "cellSegue"
        {

          let cell = tableView.cellForRowAtIndexPath(tableView.indexPathForSelectedRow()!)

          var transfer : ExplanationView = segue.destinationViewController as ExplanationView

          if let unwrapped = cell?.textLabel?.text!
          {

            transfer.infoText.text = unwrapped

          }


        }

   }

When a segue is performed, I get a fatal crash, the offending line is "transfer.infoText.text = unwrapped" and Xcode tells me it found nil when unwrapping an optional. During my debuggingg efforts, I've tried "println(cell?.textLabel?.text!)" and Xcode prints "Optional(String)" where "String" is the actual string I'm trying to access so it seems like I'm on the right track, but obviously there's something I'm missing. I've also tried "cell?.textLabel?.text" but I get the same error. Any help is appreciated

当执行 segue 时,我遇到了致命的崩溃,有问题的行是“transfer.infoText.text = unwrapped”,Xcode 告诉我它在解开可选项时发现 nil。在调试过程中,我尝试过“println(cell?.textLabel?.text!)”,Xcode 打印“Optional(String)”,其中“String”是我尝试访问的实际字符串,所以看起来我我在正确的轨道上,但显然我遗漏了一些东西。我也试过“cell?.textLabel?.text”,但我得到了同样的错误。任何帮助表示赞赏

回答by michaelsnowden

You shouldn't ever extract data from a view. You should extract data from a model. The fact that you have a tableView with cells implies you have a model for the data that goes in those cells. It might be an array of strings or whatever. Instead of trying to extract the data from the cells, just get the data from the data model you used to populate the cells. Something like:

您永远不应该从视图中提取数据。您应该从模型中提取数据。事实上,你有一个带有单元格的 tableView 意味着你有一个模型来处理这些单元格中的数据。它可能是一个字符串数组或其他任何东西。与其尝试从单元格中提取数据,不如从用于填充单元格的数据模型中获取数据。就像是:

let selectedIndexPath = tableView.indexPathForSelectedRow()
let selectedData = data[selectedIndexPath.row] 

回答by vacawama

Your problem is not unwrappedbut instead transfer.infoText.text. Your IBOutletsare not set up at the time of the prepareForSegue. You need add a property (var) to your destination view controller to hold the unwrappedvalue. Then in viewDidLoadwhen the IBOutlets are set up, copy the string to your text field.

你的问题不是unwrapped而是transfer.infoText.text。您IBOutletsprepareForSegue. 您需要var向目标视图控制器添加一个属性 ( ) 以保存该unwrapped值。然后,在viewDidLoadIBOutlets的成立,将字符串复制到文本字段。