我在TableViewController上使用PFQueryTableViewController。问题是:当我拉动刷新时数据会加载,但在视图加载时它们不会加载。
我意识到所有具有“self.tableView”的代码都不像(self.tableView.reloadData)那样。
这是我的代码:
import UIKit
class TableViewController: PFQueryTableViewController {
var titulo = String()
var sub_titulo = String()
var url = String()
var tipo = Int()
@IBOutlet weak var imagemNerdmonster: UIImageView!
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Extract values from the PFObject to display in the table cell
if let titulo = object?["titulo"] as? String {
cell?.textLabel?.text = titulo
}
if let sub_titulo = object?["sub_titulo"] as? String {
cell?.detailTextLabel?.text = sub_titulo
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let object = objectAtIndexPath(indexPath)
self.titulo = object!.objectForKey("titulo") as! String
self.sub_titulo = object!.objectForKey("sub_titulo") as! String
self.url = object!.objectForKey("url") as! String
self.tipo = object!.objectForKey("tipo") as! Int
//Chama a segue
self.performSegueWithIdentifier("detailSegue", sender: self)
}
//In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let svc = segue.destinationViewController as! DetailViewController
svc.titulo = self.titulo
svc.sub_titulo = self.sub_titulo
svc.url = self.url
svc.tipo = self.tipo
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
super.viewDidAppear(animated)
self.tableView.reloadData()
}
override func viewDidLoad() {
self.tableView.estimatedRowHeight = 100
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.reloadData()
}
@IBAction func saveButton(sender: AnyObject) {
//Return to table view
self.dismissViewControllerAnimated(true, completion: nil)
}
override func queryForTable() -> PFQuery {
let query = PFQuery(className: "Conteudo")
query.orderByAscending("createdAt")
query.limit = 20
//query.whereKey("currencyCode", equalTo:"EUR")
return query
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
//Configure the PFQueryTableView
self.parseClassName = "Conteudo"
self.textKey = "Titulo"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 20
}
}
答案 0 :(得分:1)
将此方法添加到您的课程中:
override func viewWillAppear(animated: Bool) {
loadObjects()
}