【问题标题】:TableView data not loadingTableView 数据未加载
【发布时间】:2016-03-24 18:27:37
【问题描述】:

由于某种原因,我的数据没有加载到我的表格视图中。 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
    }


}

【问题讨论】:

  • 如果使用 dispatch_async 将 reloadData 放到主队列中会怎样?
  • 哪个reloadData?更新表视图()?
  • 对不起...我的意思是在您发送给查询的块内。
  • 该解决方案不走运。你以为那会做什么?
  • 变量'team'设置在哪里? tasksRef.queryOrderedByChild("team").queryEqualToValue(team)。它被初始化为 "" 开始,但我没有看到任何代码生成空字符串以外的任何内容。

标签: ios swift uitableview firebase


【解决方案1】:

您在获取任务后似乎从未调用过updateTableView。这是为表格视图正确组织数据的方法。

【讨论】:

  • 我在 viewDidLoad 中调用了它。我还是 Swift 的新手。我还能在哪里称呼它?
  • 您需要再次调用它after您拥有所有数据。
猜你喜欢
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-05
相关资源
最近更新 更多