swift中的PFQueryTableViewController没有加载数据

时间:2015-02-19 15:21:23

标签: swift parse-platform

我正在创建一个应用程序,将数据从Parse加载到UITableView中。我使用了一些代码,似乎可以将检索到的数据加载到表格中的单元格中,但是它有问题,我做了一些研究并找到了PFQueryTableViewController。这似乎是我的问题的解决方案,但所有文档都在Objective-C(我没有任何编码经验),并且没有关于如何在Swift中使用此类的教程。有一些片段讨论如何初始化它,但没有真正通过如何在Swift中设置它。我试图通过Obj-C文档并在Swift中自己设置它,但我似乎无法做到正确并且非常感谢一些帮助。有没有人有一个建议的资源或有任何人设置一个,并可以介绍如何在Swift中执行此操作?

编辑:我找到了一些资源,并尝试设置它,表视图加载但没有任何内容加载到单元格中,我不确定。我将保留下面的代码。注释掉的部分与我的代码无关,但留待参考。关于为什么表没有加载的任何进一步的帮助将是有帮助的!

class MyCookbookVC : PFQueryTableViewController {

  override init!(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
    textKey = "Name"
    pullToRefreshEnabled = true
    paginationEnabled = true
    objectsPerPage = 25
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  override func viewDidLoad() {

    tableView.reloadData()

  }

  override func queryForTable() -> PFQuery! {
    let query = PFUser.query()
    query.whereKey("CreatedBy", equalTo: PFUser.currentUser())
    query.orderByAscending("Name")

    //if network cannot find any data, go to cached (local disk data)
    if (self.objects.count == 0){
      query.cachePolicy = kPFCachePolicyCacheThenNetwork
    }

    return query
  }

  override func tableView(tableView: UITableView!, cellForRowAtIndexPath   indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PFTableViewCell
    cell.textLabel?.text = object["Name"] as? String

    /*if let profileImage = object["profileImage"] as? PFFile {
      cell.imageView.file = profileImage
    }
    else {
      cell.imageView.image = kProfileDefaultProfileImage
    }
    cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2;
    cell.imageView.clipsToBounds = true
    cell.imageView.layer.borderWidth = 3.0
    cell.imageView.layer.borderColor = UIColor.whiteColor().CGColor */


   /* cell.textLabel?.font = UIFont(name: kStandardFontName, size: kStandardFontSize)
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.backgroundColor = kbackgroundColor */

    return cell
  }
} 

3 个答案:

答案 0 :(得分:1)

您的查询是在查询“配方”类时询问PFUser。改变这一行

let query = PFUser.query()

let query = PFQuery.queryWithClassName("Recipe")

答案 1 :(得分:0)

我在Parse Google Group中发布了我的工作解决方案。也许它会帮助你。 https://groups.google.com/forum/#!topic/parse-developers/XxLjxX29nuw

答案 2 :(得分:0)

您正在使用PFUser.query(),这对查询用户非常有用,但您想使用PFQuery(className: YOUR_PARSE_CLASS)