2016-09-29 2 views
0

보기에서 사용자가 ticket ()으로 저장 한 입력 필드에 텍스트를 입력하면이 프로세스가 문제없이 작동하고 내 데이터베이스에 성공적으로 추가됩니다).Firebase에서 tableview로 데이터 가져 오기

이 모든 ticket 값은 각 셀에 ticket이 포함 된 tableView으로 표시되기를 바랍니다. 나는 놀고 있었고 이것은 내가 갈 수있는 한 멀리이지만, 아직 충분하지 않다.

import UIKit 
import Firebase 

class TestTableViewController: UITableViewController { 

let cellNavn = "cellNavn" 

    var holes: [FIRDataSnapshot]! = [] 


let CoursesRef = FIRDatabase.database().reference().child("Ticketcontainer") 


override func viewDidLoad() { 
    super.viewDidLoad() 

    CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in 
     self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot] 
    }) 

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel)) 

    tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellNavn) 


} 


func handleCancel() { 
    dismissViewControllerAnimated(true, completion: nil) 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellNavn, forIndexPath: indexPath) as! UserCell      
    cell.textLabel?.text = self.holes[indexPath.row].value as? String 

    return cell 

} 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 72 
}  

} 

답변

0

우선 numberOfRowsInSection 메서드에서 정적 값 대신 배열의 개수를 반환해야합니다.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return holes.count 
} 

둘째 폐쇄에 배열을 initializng 후에는 tableView를 다시로드해야합니다.

CoursesRef.observeEventType(.ChildAdded, withBlock: { snapshot in 
    self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot] 
    self.tableView.reloadData() 
}) 
+0

나는 당신의 도움으로 작동하지만 어떤 이유로 든 .ChildAdded .Value로 변경해야했습니다. 나는 이제 전체 테이블을 얻는다. 그러나 나는 여전히 생각할 것이다 .Childadded는 티켓 목록에 적용된 마지막 "티켓"을 보여줄 것인가? – Frederic

+0

@ 프레드릭 당신이 말한 것을 이해하지 못해서 당신이 직면 한 문제에 대해 자세히 설명해주십시오. –

+0

죄송합니다! 처음에 빈 코드 테이블을 구현했을 때 observeEventType의 ".ChildAdded"를 ".Value"로 변경했습니다. 그리고 나서 작동했습니다. 이제는 내 데이터베이스의 모든 값을 보여줍니다. 이제 "그냥 사용자가 입력 한"티켓을 보여줄 수 있는지 궁금합니다 (thats는 .Childadded와 함께 시도한 이유지만 그냥 비어 있습니다.) 희망 더 명확하고 도움을 주셔서 감사합니다! – Frederic

관련 문제