【问题标题】:UI TableView not returning dataUI TableView 不返回数据
【发布时间】:2015-07-20 19:32:54
【问题描述】:

我有一个表格视图,单击单元格,它会进入另一个表格视图。第一个 tableview 工作并加载适当的数据。第二个不显示数据。这是我的代码:

  let query = PFQuery(className: "_User")
    query.whereKey("Chemistry", equalTo: true)
    query.findObjectsInBackgroundWithBlock { (caption: [AnyObject]?, erreur: NSError?) -> Void in
        if erreur == nil {
            // on a réussi
            for caption in caption! {
                self.chemistryListe.append(caption["username"] as! String)
            }

            println("we are here")
            println(self.chemistryListe)

        }
        else {
            // quelque chose s'est passé
        }
    }

这是我的 cellforRowatIndexPath 和 numberOfRowsinSection 方法:

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

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

    println(self.chemistryListe)
    cell.nameLabel.text = chemistryListe[indexPath.row]


    return cell
}

如果我改变这一行:

    cell.nameLabel.text = chemistryListe[indexPath.row]

到这里:

   cell.nameLabel.text = "testcell"

然后一切正常,并正确显示文本“testcell”。所以很明显,没有读到 chemistryListe 数组中有任何内容。但是我在查询中间设置了一个断点,并看到查询正确地获取了应该在数组中的数据,并将其放入数组中。但只是 tableview 单元格没有读取它。知道有什么问题吗?谢谢

【问题讨论】:

    标签: ios swift uitableview parse-platform


    【解决方案1】:

    首先确保您有数据进入数组。然后,如果数组包含字符串,则将此行更改为:

     cell.nameLabel.text = chemistryListe[indexPath.row] as! String
    

    【讨论】:

    • 我试过了,编译器把它改成了“as”,但还是不行
    • 你确定你有数据进入数组并尝试用 as 替换 as! 吗?
    • 是的,我在 for 循环之后设置了一个 println(self.chemistryListe),设置了一个断点,并查看了控制台中的日志,它确实打印了数组中应该是的 2 个元素那里。就在编译器在 cellForRowatindexPath 读取它时,它看不到任何数据。我试过 as、as! 和 as?,但这些都不起作用。但在 for 循环中,我添加了元素“as!String”,所以我认为类型应该不是问题
    • 您知道创建分配给 ** var text = chemistryListe[indexPath.row] 的内容! String** 然后执行 cell.nameLabel.text = text
    【解决方案2】:

    改变

    cell.nameLabel.text = chemistryListe[indexPath.row]
    

    cell.nameLabel.text = "\(chemistryListe[indexPath.row])"
    

    【讨论】:

    • 还是不行。我想知道它是否可能与它之前的视图控制器也包含一个表格视图这一事实有关。我目前设置它的方式是 tableView #1 中有一个单元格,然后我 crtl+clicked 并拖动以单击该单元格转换到 tableView#2,这是我遇到数组问题的地方没有出现。难道是我从一个 tableView 转换到下一个的方法是错误的?
    • 您是否将数组传递给第二个视图控制器?
    • 不,第一个 viewcontroller/table 视图中单元格的内容是通过将它们放入数组中手动​​设置的,这意味着我已经在 viewController 的顶部设置了数组的初始内容。而在 tableView #2 中,数组一开始是空的,然后通过从解析中检索数据来填充数据。我可以确认正在从 parse 中检索数据,因为 for 循环之后的 println(self.chemistryListe) 确实打印了正确的数组。
    【解决方案3】:

    自己找到了答案:

    我只需要添加

       self.tableView.reloadData()
    

    到查询方法的末尾。

    感谢大家的努力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多