ios - 使用 swift 2 在 tableview 上处理单元格

标签 ios swift uitableview swift2 uistoryboardsegue

我正在使用 Swift 2 开发一个应用程序。在我的应用程序中,我将 JSON 数据解析为 TableView 。当我点击一个单元格时,它成功移动到另一个 View Controller ,但我无法获取 json 数据。

这是我的 View Controller 中的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath){
    var cell = self.TableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! CustomTableCell
    let strbookdetail : NSString=arrDict[indexPath.row] .valueForKey("booking_detail") as! NSString
    let strdrop : NSString=arrDict[indexPath.row] .valueForKey("drop_address") as! NSString
    cell.Bookdetail.text=strbookdetail as String
    cell.Dropaddress.text=strdrop as String
    return cell as CustomTableCell

}

//It helps to parse JSON  data to table view.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    TableView.deselectRowAtIndexPath(indexPath, animated: true)
    let tripcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("UpcomingController") as! Secondcontroller
    navigationController?.pushViewController(tripcontroller, animated: true)

}

点击单元格时,它有助于移动到其他 View Controller SecondController

在我的 SecondController 中,我有两个 UILabel。在这些标签中,我想显示数据,即 strbookdetailstrdrop

如何在两个 View Controller 之间传递引用?

最佳答案

使用property传递数据很容易做到:

在第二个vc中:

var dataFromBeforeVC:[String:String] = [String:String]()

override func viewDidLoad() {
    super.viewDidLoad()


    initData ()

}

func initData()  {

    // set data  strbookdetail and strdrop to labelOne & labelTwo

    labelOne.text = dataFromBeforeVC["strdrop"]
    labelTwo.text = dataFromBeforeVC["strbookdetail"]


}

在第 1 个 vc 中:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    TableView.deselectRowAtIndexPath(indexPath, animated: true)
    let tripcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("UpcomingController") as! Secondcontroller

    let cell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!

    let strbookdetail : NSString=arrDict[indexPath.row] .valueForKey("booking_detail") as! NSString
    let strdrop : NSString=arrDict[indexPath.row] .valueForKey("drop_address") as! NSString
    tripcontroller.dataFromBeforeVC = [
        "strdrop":strbookdetail,
        "strdrop":strbookdetail
    ]

    self.navigationController?.pushViewController(tripcontroller, animated: true)
}

关于ios - 使用 swift 2 在 tableview 上处理单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40736213/

相关文章:

android - 在 React Native 中保存时旋转图像的问题

ios - 如何获取图层名称以在 iOS 示例应用程序中使用? ( tensorflow )

ios - 在 View 中快速单击 + 按钮添加新文本字段

swift - 获取 JSON,将其解析为数组,然后在 Swift 中打印

ios - Objective-c 项目 : not supported for static libraries 中的 Swift

ios - 如何让 "View All"按钮通过 segue 到下一个 ViewController 显示数据?

ios - 更新父类(super class)的 TableView

ios - 根据方向处理状态栏高度

ios - 在 Swift 中读取 JSON Key 0

iOS Swift - 自定义 UITableViewCell 不适用于标签栏