2016-08-15 77 views
1

我的应用程序从Firebase数据库检索数据,使用我的CustomCell将其放入tableView中,我的目标是当单元格被点击时,我想要存储在标签中的字符串那个窃听的细胞。访问来自Firebase in didSelectRowAtIndexPath的CustomCell内容

所以我现在正在尝试这几天,所提供的解决方案大多只适用于静态单元格,其中内容不动态加载,就像它在这里。 选项我想:

  1. 使用数组来存储内容,然后用indexPath让他们的组合:事实证明,这是困难的,因为阵列无法与火力地堡同步,导致在indexPath错误的订单。我还没有找到一个保持数组同步的库,但是一般的共识似乎是在使用Firebase时我应该远离数组。
  2. 使用代表团或类似建议here关闭:因为我现在用的是FirebaseUI来填充我的电池,我没有这两种方法cellForRowAtIndexPathdequeueReusableCellWithIdentifier,这也让一个困难的解决方案。

我到目前为止有:

func getQuery() -> FIRDatabaseQuery { 
let myTopPostsQuery = (ref.child("posts")) 

return myTopPostsQuery 
} 

override func viewDidLoad() { 
super.viewDidLoad() 


self.ref = FIRDatabase.database().reference() 

dataSource = FirebaseTableViewDataSource.init(query: getQuery(),prototypeReuseIdentifier: "Cellident", view: self.tableView) 


dataSource?.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in 

    let customCell = cell as! CustomCellTableViewCell 
    let snap = obj as! FIRDataSnapshot 
    let childString = snap.value as! [String : AnyObject] 

    customCell.textLabel.text = String(childString)} // << This is what I need to access in didSelectRowAtIndexPath 

} 

    tableView.dataSource = dataSource 
    tableView.delegate = self 

} 

我的问题是现在是什么将是一段最实用的方式,如果有一个解决方案,我还没有考虑?

回答

1

在didSelectRowAtIndexPath方法TableViewDelegate方法做到这一点

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let path: NSIndexPath = indexPath 
    let source = dataSource 
    let snapshot: FIRDataSnapshot = (source?.objectAtIndex(UInt(path.row)))! as! FIRDataSnapshot 
    let childString = snapshot.value as! String 
}