简体   繁体   English

如何在Swift中将数据从一个表视图传递到另一个表视图?

[英]How to pass data from one table view to another table view in Swift?

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView
    var dict1:Dictionary<Int,String> = [ 0:"One",1:"TwO",2:"Three"];

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.dict1.count;
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel.text = self.dict1[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)!")

    }               
}

You can explicitly build the new view controller and pass the data in when you detect that a row is selected: 您可以显式构建新的视图控制器,并在检测到选择了某行时将数据传递给它:

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath path: NSIndexPath!) {
    let entry = news[path.row] as NSDictionary
    let url = entry["link"] as NSString
    let secondViewController = 
        self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController")
        as SecondViewController

    // pass the relevant data to the new sub-ViewController
    secondViewController.url=url;
    // tell the new controller to present itself
    self.navigationController.pushViewController(secondViewController, animated: true)
}

and in SecondViewController.swift, add a variable to hold the relevant data. 在SecondViewController.swift中,添加一个变量来保存相关数据。

class SecondViewController: UIViewController {
    var url: String? = ""
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速将数据从解析表传递到表视图? - How to pass data from parse table into table view in swift? 将json ID从一个表视图控制器传递到Swift 3中的另一个表视图控制器 - pass json id from one table view controller into another table view controller in swift 3 如何将数据从表视图传递到另一个视图 controller - How to pass the data from table view to another view controller 如何将不同的数据从表视图传递到另一个视图 controller - How to pass the different data from table view to another view controller Swift-将数据从表视图Xib文件传递到视图控制器 - Swift - Pass data from table view xib file to view controller 如何在Swift中从另一个视图访问视图的表格视图的单元格? - How to access cells of a table view of a view from another view in Swift? 表格视图帮助.....从一个表格视图中选择的单元格迅速传递到另一个表格视图 - Table view Help…..selected cells from one table view passed to another table view swift 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 以编程方式将数据从一个视图快速传递到另一个视图 - Pass data from one view to another in swift programatically Swift:将数据传递给静态表视图 - Swift: Pass data to static table view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM