我在iOS应用中使用Firebase和Swift。我有2个带有tableviews的视图控制器。我想在单击单元格的附件按钮时将数据从tableview单元格(即postkey)传递到下一个视图控制器。我想从url https://xxxx.firebaseio.com/users/(uid)/posts/(postkey)/offers中检索数据,并在第二个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()
}
}
第二个tableview没有显示任何数据。希望检查我的代码中是否存在从Firebase检索数据的错误。
答案 0 :(得分:0)
您需要更正以下代码。
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变量添加到第二个视图Controller
中var postKey:String?