简体   繁体   English

使用Swift 2在TableView上使用单元格

[英]working with cell on tableview using swift 2

I am developing a app using Swift 2. In my app I am parsing JSON data to a table view. 我正在使用Swift 2开发一个应用程序。在我的应用程序中,我正在将JSON数据解析为表视图。 When I tap a cell it successfully moves to another view controller but I am unable to fetch the json data. 当我点击一个单元格时,它成功移动到另一个视图控制器,但是我无法获取json数据。

This is the code in my 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)

}

When tapping on the cell it helps to move to other view controller SecondController 当点击单元格时,它有助于移动到其他视图控制器SecondController

In my SecondController I have two UILabel s. 在我的SecondController我有两个UILabel In those labels I want to show data ie strbookdetail and strdrop 在这些标签中,我想显示数据,即strbookdetailstrdrop

How can I pass a reference between the two view controllers? 如何在两个视图控制器之间传递引用?

It is easy to do using property pass data: 使用property传递数据很容易做到:

In 2th vc : 在2 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"]


}

In 1th vc : 在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)
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM