2016-08-15 72 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 
}