简体   繁体   English

将变量传递给另一个视图控制器并在表视图中显示数据

[英]Passing a variable to another view controller and showing data in a tableview

I'm using Firebase and Swift in my iOS app.我在我的 iOS 应用中使用 Firebase 和 Swift。 I have 2 view controllers with tableviews.我有 2 个带有 tableviews 的视图控制器。 I want to pass data from the tableview cell (ie, postkey) to the next view controller upon clicking the cell's accessory button.我想在单击单元格的附件按钮时将数据从 tableview 单元格(即 postkey)传递到下一个视图控制器。 And I want to retrieve the data from the url https://xxxx.firebaseio.com/users/(uid)/posts/(postkey)/offers and display all the offers that correspond to the postKey in the second tableview.我想从 url https://xxxx.firebaseio.com/users/(uid)/posts/(postkey)/offers 中检索数据,并在第二个 tableview 中显示与 postKey 对应的所有优惠。 This is the code for the first view controller:这是第一个视图控制器的代码:

 func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! FavorCell
    postKey = currentCell.postKey.text
    performSegueWithIdentifier("seguetoAccept", sender: self)
}


 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let viewControllerAccept = segue.destinationViewController as! AcceptVC
    viewControllerAccept.postKey = postKey
 //passing the postKey to AcceptVC's postKey    
}

And this one's the code for the second view controller:这是第二个视图控制器的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    bidTableView.delegate = self
    bidTableView.dataSource = self

    let bidCell = Firebase(url:"https://xxxx.firebaseio.com/users/\(uid)/posts/\(postkey)/offers")


    bidCell.observeEventType(.Value, withBlock: { snapshot in
        print(snapshot.value)
        self.favors = []


        if let snapshots = snapshot.children.allObjects as? [FDataSnapshot]{

            for snap in snapshots {
                print("SNAP: \(snap)")
                if let postdic = snap.value as? Dictionary <String, AnyObject>{
                    let key = snap.key
                    let offer = Offer(bidKey: key, dictionary: postdic)

                    self.favors.append(offer)

                    } 
                }
            }
        self.bidTableView.reloadData()
    })

    }


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return favors.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let favor = favors[indexPath.row]
    if let cell = tableView.dequeueReusableCellWithIdentifier("acceptCell") as? AcceptCell {
        var img: UIImage?
        cell.configureCell(favor)
        return cell
    } else {
        return AcceptCell()
    }
}

The second tableview isn't showing any data.第二个 tableview 没有显示任何数据。 Wish to check if there's an error in my code insofar as retrieving data from Firebase is concerned.就从 Firebase 检索数据而言,希望检查我的代码中是否存在错误。

You need to correct your code as below.您需要按如下方式更正您的代码。

func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! FavorCell
    postKey = currentCell.postKey.text
    performSegueWithIdentifier("seguetoAccept", sender: postKey)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "seguetoAccept" {
        let viewControllerAccept = segue.destinationViewController as! AcceptVC
        let postKey = sender as? String
        viewControllerAccept.postKey = postKey
    }

    //passing the postKey to AcceptVC's postKey
}

You also need to add postKey variable into second view Controller您还需要将 postKey 变量添加到第二个视图控制器中

var postKey:String?

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

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