2016-10-01 38 views
1

我有两个tableView在我的项目中运行。我试图传递(复制)我的第一个tableViewcell数据到第二个tableView.I使用tableView行动作方法传递data.My部分代码在下面.. 。在Swift中在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 
} 

二VC:

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 ..

+1

你为什么要调用'self.performSegue(withIdentifier:“send”,sender:nil)'里面准备继续? –

+0

[将数据从TableView发送到DetailView Swift]的可能的副本(http://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift) –

+0

@Nirav D.谢谢。我将发件人更改为自我。仍然没有结果.. – Joe

回答

1

好吧,测试它后,事实证明,你正在使用不正确的prepareForSegue函数。你没有使用“prepareForSegue”,你是创建函数prepareForSegue - 因为语法在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])   
    } 
} 

你也需要通过indexPath:

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] 
} 

这个工作在我的测试项目。

另外要注意:CellUITableViewCell的一个自定义子类,我创建的标签元素是我的测试项目的标签元素的UIOutlet