2016-03-24 10 views
0

何らかの理由で、私のデータがテーブルビューに読み込まれません。 fetchTasks関数は動作しているようです。なぜなら、ブレークポイントを追加すると、tasksには151個のタスクがありますが、fetchTasks関数の外のどこからでもタスクを試してみると、タスクは空の配列であることがわかります。私はこれが私のupdateTableView機能が機能していない理由と推測します。これらの変数をそれぞれ出力すると、空の配列も得られます。 Xcodeでエラーが発生していないので、これを少しでもイライラさせることができません。どんな助けも素晴らしいだろう。私はすでにグーグルグーグルをしてきました。前もって感謝します。TableViewデータがロードされていません

class TasksTVC: UITableViewController, TaskCellDelegate { 


    let ref = Firebase(url: URL) 
    var cell = TaskCell() 
    var team = "" 
    var sectionTimes = [String]() 
    var tasksInSectionArray = [[Task]]() 
    var tasks = [Task]() { 
     didSet { 
      tableView?.reloadData() 
     } 
    } 

    func fetchTasks() { 
     let tasksRef = Firebase(url: "\(self.ref)/tasks") 
     tasksRef.queryOrderedByChild("team").queryEqualToValue(team).observeEventType(.Value, withBlock: { snapshot in 
      var newTasks = [Task]() 
      for task in snapshot.children { 
       let tasks = Task(snapshot: task as! FDataSnapshot) 
       newTasks.append(tasks) 
      } 
      self.tasks = newTasks 
      self.tableView.reloadData() 
     }) 
    } 

    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 
     fetchTasks() 
     } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     updateTableView() 
     self.tableView.rowHeight = UITableViewAutomaticDimension 
     self.tableView.estimatedRowHeight = 44.0 
    } 

    func updateTableView() { 
     sectionTimes = Set(tasks.map{$0.dueTime}).sort() 
     tasksInSectionArray = sectionTimes.map{section in tasks.filter{$0.dueTime == section}} 
     print("section times:\(sectionTimes)") 
     print(tasksInSectionArray) 
     tableView.reloadData() 
    } 


    // MARK: - Table view data source 

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return sectionTimes[section] 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return sectionTimes.count 
    } 

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


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! TaskCell 

     // Configure the cell... 
     cell.selectionStyle = .None 
     let task = tasksInSectionArray[indexPath.section][indexPath.row] 
     cell.label.text = task.title 
     if task.done == true { 
      cell.checkBox.image = UIImage(named: "checkedbox") 
      cell.detailLabel.text = "Completed By: \(task.completedBy)" 
      } 
      else { 
      cell.checkBox.image = UIImage(named: "uncheckedbox") 
      cell.detailLabel.text = "" 
      } 

     cell.delegate = self 
     return cell 
    } 


} 
+0

dispatch_asyncを使用してreloadDataをメインキューに入れるとどうなりますか? –

+0

どのreloadDataですか? Updatetableview()? –

+0

申し訳ありません...私はあなたがクエリに送信するブロックの内側を意味します。 –

答えて

0

タスクをフェッチした後に決してupdateTableViewと呼んでいるようです。これは、テーブルビューのデータを正しく整理する方法です。

+0

私はそれをviewDidLoadで呼び出しました。私はまだスウィフトの新人です。それ以外のところはどこですか? –

+0

あなたはあなたのすべてのデータを '後で'それをもう一度呼び出す必要があります。 – laynemoseley

関連する問題