Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
在Swift中的TableView之间传递数据_Swift_Uitableview - Fatal编程技术网

在Swift中的TableView之间传递数据

在Swift中的TableView之间传递数据,swift,uitableview,Swift,Uitableview,我的项目中运行了两个tableView。我正在尝试将第一个tableView单元格数据传递(复制)到第二个tableView。我使用tableView行操作方法传递数据。下面的部分代码 第一个VC: var tableView: UITableView! var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S cooter","Caravan"] v

我的项目中运行了两个tableView。我正在尝试将第一个tableView单元格数据传递(复制)到第二个tableView。我使用tableView行操作方法传递数据。下面的部分代码

第一个VC:

var tableView: UITableView!
var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S    cooter","Caravan"]
var sendSelectedData = NSString()

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in

print("Button Pressed") // Xcode Console prints **Button Pressed** when swipe action performed.

self.performSegue(withIdentifier: "send", sender: self)

 }

return [copyAction]
 }


func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    self.performSegue(withIdentifier: "send", sender: self)
    // segue.destination as! tableController

    let indexPath = tableView.indexPathForSelectedRow
    let currentCell =  tableView.cellForRow(at: indexPath!)!
    self.sendSelectedData = (currentCell.textLabel?.text)! as String as NSString

    let viewController = segue.destination as! tableController
    viewController.labelcell = ([self.sendSelectedData as String])
    print(self.sendSelectedData)  // no result
}
var labelcell = [String]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath as IndexPath) as UITableViewCell


    cell.textLabel?.text = labelcell[indexPath.row] as? String

    tableView.reloadData()

    return cell
}
第二个VC:

var tableView: UITableView!
var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S    cooter","Caravan"]
var sendSelectedData = NSString()

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in

print("Button Pressed") // Xcode Console prints **Button Pressed** when swipe action performed.

self.performSegue(withIdentifier: "send", sender: self)

 }

return [copyAction]
 }


func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    self.performSegue(withIdentifier: "send", sender: self)
    // segue.destination as! tableController

    let indexPath = tableView.indexPathForSelectedRow
    let currentCell =  tableView.cellForRow(at: indexPath!)!
    self.sendSelectedData = (currentCell.textLabel?.text)! as String as NSString

    let viewController = segue.destination as! tableController
    viewController.labelcell = ([self.sendSelectedData as String])
    print(self.sendSelectedData)  // no result
}
var labelcell = [String]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath as IndexPath) as UITableViewCell


    cell.textLabel?.text = labelcell[indexPath.row] as? String

    tableView.reloadData()

    return cell
}

上面的代码看起来像是将数据传递给我的第二个VC(segue)。但是,我只得到了一个空的tableview。

测试后,结果表明,您使用了一个不正确的
prepareForSegue
函数。您没有使用“prepareforsgue”,而是创建了一个名为
prepareforsgue
的函数,因为Swift 3中的语法已更改。这个将被调用,您可以传递数据

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "send" {

        let selectedIndex = sender as! NSIndexPath
        let currentCell =  tableView.cellForRow(at: selectedIndex as IndexPath)! as! Cell
        self.sendSelectedData = (currentCell.label?.text)! as String as NSString

        print(self.sendSelectedData) // till here it worked for me - it is filled with my label.text
        // I don't know what this is "viewController.labelcell", so you have to to know how to go on from here on

        viewController.labelcell = ([self.sendSelectedData as String])           
    }
}
此外,您还需要传递索引XPath:

self.performSegue(withIdentifier: "send", sender: indexPath)
正是这样:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in

        print("editActionsForRowAt called") // Xcode Console prints **Button Pressed** when swipe action performed.
        self.performSegue(withIdentifier: "send", sender: indexPath)

    }

    return [copyAction]
}
这在我的测试项目中起了作用


还要注意:
Cell
是我创建的
UITableViewCell
的自定义子类,label是我测试项目标签元素的
UIOutlet

为什么要调用
self.performsgue(标识符:“send”,发送者:nil)
内部准备segue?可能是@Nirav D的重复。谢谢我把发件人改成了self。仍然没有结果。@DavidSeek我已经看过你的链接,我在论坛上也看到了类似的问题。所有这些问题只解释了如何将数据传递给detailview控制器,而不是电视。您是否发现我的代码有任何错误。。。。thanks@DavidSeek此打印将被称为致命错误:在tableView.indexPathForSelectedRow中展开可选值(lldb)时意外发现nil。