Ios TableView数据未加载
由于某些原因,我的数据没有加载到我的tableview中。Ios TableView数据未加载,ios,swift,uitableview,firebase,Ios,Swift,Uitableview,Firebase,由于某些原因,我的数据没有加载到我的tableview中。fetchTasks函数似乎正在工作,因为如果我向其添加断点,它会告诉我tasks中有151个任务,但当我尝试从fetchTasks函数之外的任何位置打印任务时,它会告诉我任务是一个空数组。我假设这就是我的updateTableView函数无法工作的原因。当我打印这些变量时,它也会给我一个空数组。我在Xcode中没有收到任何错误,所以我不能让它工作有点令人沮丧。任何帮助都会很好。我已经用谷歌搜索了很多次了。提前谢谢 class Tasks
fetchTasks
函数似乎正在工作,因为如果我向其添加断点,它会告诉我tasks
中有151个任务,但当我尝试从fetchTasks
函数之外的任何位置打印任务时,它会告诉我任务是一个空数组。我假设这就是我的updateTableView
函数无法工作的原因。当我打印这些变量时,它也会给我一个空数组。我在Xcode中没有收到任何错误,所以我不能让它工作有点令人沮丧。任何帮助都会很好。我已经用谷歌搜索了很多次了。提前谢谢
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
}
}
在获取任务后,您似乎从未调用过
updateTableView
。这就是为表视图正确组织数据的方法。如果使用dispatch\u async将重载数据放入主队列,会怎么样?哪个重载数据?Updatetableview()?抱歉……我是说在您发送到查询的块内。该解决方案不成功。你认为这会做什么?变量“team”集在哪里?tasksRef.queryOrderedByChild(“团队”)。QueryQualtOvalue(团队)。开始时它被初始化为“”,但除了空字符串之外,我看不到任何代码。我在viewDidLoad中调用了它。我对斯威夫特还是新手。我还可以在哪里调用它?您需要在拥有所有数据后再次调用它。