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

Ios 将变量传递给另一个视图控制器并在tableview中显示数据,ios,swift,uitableview,Ios,Swift,Uitableview,我在iOS应用程序中使用Firebase和Swift。我有两个带有表视图的视图控制器。单击单元格的附件按钮后,我希望将数据从tableview单元格(即postkey)传递到下一个视图控制器。我想从url中检索数据,并在第二个tableview中显示与postKey对应的所有提供。这是第一个视图控制器的代码: func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NS

我在iOS应用程序中使用Firebase和Swift。我有两个带有表视图的视图控制器。单击单元格的附件按钮后,我希望将数据从tableview单元格(即postkey)传递到下一个视图控制器。我想从url中检索数据,并在第二个tableview中显示与postKey对应的所有提供。这是第一个视图控制器的代码:

 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    
}
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()
    }
}
这是第二个视图控制器的代码:

 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    
}
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()
    }
}
override func viewDidLoad(){
super.viewDidLoad()
bidTableView.delegate=self
bidTableView.dataSource=self
让bidCell=Firebase(url:)https://xxxx.firebaseio.com/users/\(uid)/posts/\(postkey)/offers)
bidCell.observeEventType(.Value,withBlock:{中的快照
打印(快照值)
self.favors=[]
如果让快照=snapshot.children.allObjects为?[FDataSnapshot]{
用于管理单元快照{
打印(“快照:\(快照)”)
如果让postdic=snap.value作为字典{
让key=snap.key
让报价=报价(bidKey:key,dictionary:postdic)
self.favors.append(offer)
} 
}
}
self.bidTableView.reloadData()
})
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
报恩
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让偏好=偏好[indexPath.row]
如果let cell=tableView.dequeueReusableCellWithIdentifier(“acceptCell”)为?acceptCell{
var-img:UIImage?
cell.configureCell(偏好)
返回单元
}否则{
返回AcceptCell()
}
}

第二个tableview没有显示任何数据。希望检查我的代码在从Firebase检索数据时是否有错误。

您需要按以下方式更正代码

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
}
您还需要将postKey变量添加到第二个视图控制器中

var postKey:String?

希望检查我的代码有什么问题…第二个视图控制器没有显示任何数据。第二个控制器是否为
postkey
获取了正确的值?是的,我在控制台中打印了它…它是正确的值。好的,因此传递变量不是问题的一部分。在调用
self.bidTableView.reloadData()
时,
偏好数组包含什么?另外,您是否看到
打印(“SNAP:\(SNAP)”)
的输出?不幸的是,没有输出…控制台读取:无法同时满足约束。