【问题标题】:Passing Data between TableView in Swift在 Swift 中的 TableView 之间传递数据
【发布时间】:2017-02-10 01:08:41
【问题描述】:

我的项目中有两个 tableView 正在运行。我正在尝试将我的第一个 tableViewcell 数据传递(复制)到第二个 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
}

第二个 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..

【问题讨论】:

  • 你为什么要在里面打电话self.performSegue(withIdentifier: "send", sender: nil)准备转场?
  • @Nirav D 。谢谢。我将发件人更改为自己。还是没有结果..
  • @DavidSeek 我已经查看了您的链接,并且在论坛中也看到了类似的问题。所有这些问题只解释了如何将数据传递给 detailview 控制器而不是 TV。你发现我的代码有什么问题吗....谢谢
  • @DavidSeek THIS PRINT WILL GET CALLED 致命错误:在 tableView.indexPathForSelectedRow 展开可选值 (lldb) 时意外发现 nil。

标签: swift uitableview


【解决方案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]
}

这在我的测试项目中有效。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-12
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多