Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中使用prepareForSegue方法传递自定义单元格的内容_Ios_Swift_Uitableview - Fatal编程技术网

Ios 在Swift中使用prepareForSegue方法传递自定义单元格的内容

Ios 在Swift中使用prepareForSegue方法传递自定义单元格的内容,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个带有自定义单元格的UITableView,其中包含一个标签。我想要的是,当选定行时,能够将此标签的内容传递给第二个视图控制器。 我的代码在没有自定义单元格的表上运行良好: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithId

我有一个带有自定义单元格的
UITableView
,其中包含一个标签。我想要的是,当选定行时,能够将此标签的内容传递给第二个视图控制器。
我的代码在没有自定义单元格的表上运行良好:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell

    //labelDisplayLedQty is the name of the custom label
    cell.labelDisplayLedQty.text = inputLedQty.text

    return cell
}
这是我在
prepareforsgue
方法中的代码。正如我所说的,如果我在不包含自定义单元格的表中使用该代码,则该代码可以正常工作

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

    let rowIndex: NSIndexPath = tableList.indexPathForSelectedRow!
    let selectedRow: UITableViewCell = tableList.cellForRowAtIndexPath(rowIndex)!
    let contentFromSelectedRow: String = selectedRow.textLabel!.text!

    if let secondVC = segue.destinationViewController as? DriverDetailsViewController where
       segue.identifier == "segueDriverDetails" {
        secondVC.messageContentFromMainController = contentFromSelectedRow + " This message is from main viewController"
    }
}
使用上面的代码,当我选择一行时,会出现以下错误,这是有意义的,因为我没有使用默认的textLabel:

fatal error: unexpectedly found nil while unwrapping an Optional value
我试着改变

let contentFromSelectedRow: String = selectedRow.textLabel!.text!

但随后我得到以下提示错误消息:

Value of type 'UITableViewCell has no member labelDisplayLedQty'
有人能告诉我需要做什么才能将所选行的内容传递给第二个视图控制器吗


谢谢

您说过,您正在使用一个自定义的
UITableViewCell
。因此,您必须强制转换
selectedRow
以获得该自定义单元格的类型。试试这个:

let selectedRow = tableList.cellForRowAtIndexPath(rowIndex)! as! CustomTableViewCellName

这将使您能够访问自定义标签。

问题出在以下几行:

let selectedRow:UITableViewCell = tableList.cellForRowAtIndexPath(rowIndex)!
let contentFromSelectedRow:String = selectedRow.textLabel!.text!
selectedRow
不应是
UITableViewCell
,因为您使用的是自定义单元格

这应该解决:

let selectedRow = tableList.cellForRowAtIndexPath(rowIndex) as! CustomCell
let contentFromSelectedRow:String = selectedRow.labelDisplayLedQty.text

由于表视图单元格是自定义单元格,因此必须强制转换类型

let selectedRow = tableList.cellForRowAtIndexPath(rowIndex) as! CustomCell

注意:在
prepareforsgue
中调用
cellforrowatinexpath
始终是最糟糕的选择。第一种选择是从模型(数据源数组)而不是从视图(表格视图单元格)中获取数据。

首先,您会在
CustomCell
中得到错误,但不会在默认单元格类中得到错误。为什么?让我们看看您的代码:

let selectedRow: UITableViewCell = tableList.cellForRowAtIndexPath(rowIndex)!
上面的代码获取UITableViewCell类,而不是自定义类。但是您创建了CustomClass单元格,而不是UITableViewCell。这就是为什么它开始变得越来越令人困惑。首先,您必须指定正在使用的类。但仍然要使用
不是您想要的代码,我会使用
if let
甚至
guard
语句

guard let selectedRow = tableList.cellForRowAtIndexPath(rowIndex) as? CustomCell else {
    return
}
其次,您现在可以为自定义类使用属性。如您所见,无法获取属性
labeldisplayedqty
,因为它不是UITableViewCell的属性现在您可以使用它了。您的自定义单元格没有
textLabel
属性,因此它也无法工作

let contentFromSelectedRow: String = selectedRow.labelDisplayLedQty!.text
第三个,我将测试您是否总是获得indexPath,因为您再次使用
展开可选的。下面的线不太安全(您可以使用防护装置,或者如果再次使用,请使用):


最后一个,但并非最不重要的一点是-您可以通过访问单元格的不同方式访问数据源-使用填充单元格的数组。

如果所有单元格都创建为
CustomCell
,还可以是什么?在这种情况下,隐式unwrapped optional是绝对安全的,可选绑定是不必要的开销。现在是(可能不是,取决于他的代码),但将来可能会导致问题。您应该始终用代码展望未来。无意冒犯,但这不是Swift的设计目的。好的,您正在深入思考,这里不需要它。但是谢谢你抽出时间。首先谢谢你。嗯,很抱歉,我不知道如何通过在PrepareForsgue中调用
CellForRowatineXpath
来解决这个问题。在
CellForRowatineXpath
中,您正在用
label.text=datasource[indexath.row]
更新视图(例如标签)。如果您需要标签的值,只需在
prepareforsgue
中执行相同的操作:从数据源获取值。感谢您的建议,事实上,您的建议解决了问题。更改以下两行修复了此问题:
let selectedRow=tableList.cellforRowatinexPath(rowIndex)!作为!CustomCell
让contentFromSelectedRow:String=selectedRow.LabelDisplayedQty!。短信完全正确。看来你已经明白这个问题了。
let contentFromSelectedRow: String = selectedRow.labelDisplayLedQty!.text
let rowIndex: NSIndexPath = tableList.indexPathForSelectedRow!