swift 中的 PFQueryTableViewController 未加载数据
PFQueryTableViewController in swift not loading data
我正在创建一个将数据从 Parse 加载到 UITableView 的应用程序。我使用了一些代码似乎可以将检索到的数据加载到 table 中的单元格中,但它有问题,我做了一些研究并找到了 PFQueryTableViewController。这似乎是我的问题的解决方案,但所有文档都在 Objective-C 中(我没有任何编码经验),而且我找不到关于如何使用此 [=17] 的教程=] 在 Swift 中。在 Swift 中,有一些片段讨论了如何初始化它,但没有真正介绍如何设置它。我试图浏览 Obj-C 文档并在 Swift 中自行设置它,但我似乎无法正确设置它,非常感谢您的帮助。有没有人有建议的资源或有没有人设置一个并且可以在 Swift 中逐步介绍如何做到这一点?
编辑:我找到了一些资源并尝试设置它,table 视图加载但没有任何内容加载到单元格中,我不确定。我将在下面留下代码。注释掉的部分与我的代码无关,但留作参考。关于为什么 table 没有加载的任何进一步帮助都会有所帮助!
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
}
}
我在 Parse Google 组中发布了我的工作解决方案。也许它会对你有所帮助。
https://groups.google.com/forum/#!topic/parse-developers/XxLjxX29nuw
您的查询正在查询 PFUser,而它应该查询您的 "Recipe" class。更改此行
let query = PFUser.query()
至
let query = PFQuery.queryWithClassName("Recipe")
您正在使用 PFUser.query()
,这对查询用户很有用,但您想使用 PFQuery(className: YOUR_PARSE_CLASS)
我正在创建一个将数据从 Parse 加载到 UITableView 的应用程序。我使用了一些代码似乎可以将检索到的数据加载到 table 中的单元格中,但它有问题,我做了一些研究并找到了 PFQueryTableViewController。这似乎是我的问题的解决方案,但所有文档都在 Objective-C 中(我没有任何编码经验),而且我找不到关于如何使用此 [=17] 的教程=] 在 Swift 中。在 Swift 中,有一些片段讨论了如何初始化它,但没有真正介绍如何设置它。我试图浏览 Obj-C 文档并在 Swift 中自行设置它,但我似乎无法正确设置它,非常感谢您的帮助。有没有人有建议的资源或有没有人设置一个并且可以在 Swift 中逐步介绍如何做到这一点?
编辑:我找到了一些资源并尝试设置它,table 视图加载但没有任何内容加载到单元格中,我不确定。我将在下面留下代码。注释掉的部分与我的代码无关,但留作参考。关于为什么 table 没有加载的任何进一步帮助都会有所帮助!
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
}
}
我在 Parse Google 组中发布了我的工作解决方案。也许它会对你有所帮助。 https://groups.google.com/forum/#!topic/parse-developers/XxLjxX29nuw
您的查询正在查询 PFUser,而它应该查询您的 "Recipe" class。更改此行
let query = PFUser.query()
至
let query = PFQuery.queryWithClassName("Recipe")
您正在使用 PFUser.query()
,这对查询用户很有用,但您想使用 PFQuery(className: YOUR_PARSE_CLASS)