簡體   English   中英

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

[英]working with cell on tableview using swift 2

我正在使用Swift 2開發一個應用程序。在我的應用程序中,我正在將JSON數據解析為表視圖。 當我點擊一個單元格時,它成功移動到另一個視圖控制器,但是我無法獲取json數據。

這是我的視圖控制器中的代碼:

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)

}

當點擊單元格時,它有助於移動到其他視圖控制器SecondController

在我的SecondController我有兩個UILabel 在這些標簽中,我想顯示數據,即strbookdetailstrdrop

如何在兩個視圖控制器之間傳遞引用?

使用property傳遞數據很容易做到:

在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"]


}

在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