gpt4 book ai didi

ios - TableView数据未加载

转载 作者:行者123 更新时间:2023-11-30 13:34:43 25 4
gpt4 key购买 nike

由于某种原因,我的数据没有加载到我的表格 View 中。 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。这是为 TableView 正确组织数据的方法。

关于ios - TableView数据未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36207471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com