简体   繁体   中英

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

I'm using Firebase and Swift in my iOS app. I have 2 view controllers with tableviews. I want to pass data from the tableview cell (ie, postkey) to the next view controller upon clicking the cell's accessory button. 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. 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. Wish to check if there's an error in my code insofar as retrieving data from Firebase is concerned.

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

var postKey:String?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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