除非您进行刷新,否则我的代码中的数据表不会更新。我需要tableview在视图加载时立即更新。我对此感到困惑,而且每当我更改分段控制器上的选择时,我都需要更新数据,而不必在选择特定段以获取新数据后总是拉动刷新。以下是我的tableViewController代码:
class TableViewController: PFQueryTableViewController, CLLocationManagerDelegate {
@IBOutlet weak var controller: UISegmentedControl!
var yaks = ["a","b","c", "d", "e"]
var comments: [String]?
let locationManager = CLLocationManager()
var currLocation : CLLocationCoordinate2D?
override init!(style: UITableViewStyle, className: String!)
{
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "Yak"
self.textKey = "text"
self.pullToRefreshEnabled = true
self.objectsPerPage = 200
self.paginationEnabled = false
}
private func alert(message : String) {
let alert = UIAlertController(title: "Ooops !!", message: message, preferredStyle: UIAlertControllerStyle.Alert)
let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
let settings = UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default) { (action) -> Void in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
return
}
alert.addAction(settings)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
alert("Ooops we are not able to get your location !!")
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
locationManager.stopUpdatingLocation()
if(locations.count > 0){
let location = locations[0] as CLLocation
println(location.coordinate)
currLocation = location.coordinate
} else {
alert("Ooops we are not able to get your location !!")
}
}
override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
var obj : PFObject? = nil
if(indexPath.row < self.objects.count){
obj = self.objects[indexPath.row] as PFObject
}
if(obj?.objectForKey("comments") != nil) {
comments = obj?.objectForKey("comments") as [String]
}
return obj
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 96
self.tableView.rowHeight = UITableViewAutomaticDimension
locationManager.desiredAccuracy = 1000
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let queryLoc = currLocation
}
override func queryForTable() -> PFQuery! {
let query = PFQuery(className: "posts")
if let queryLoc = currLocation
{
if controller.selectedSegmentIndex == 0 {
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: queryLoc.latitude, longitude: queryLoc.longitude), withinMiles: 4)
query.limit = 200;
query.orderByDescending("createdAt")
}
if controller.selectedSegmentIndex == 1
{
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: queryLoc.latitude, longitude: queryLoc.longitude), withinMiles: 4)
query.limit = 200;
query.orderByDescending("commentsNo")
}
}
else {
if controller.selectedSegmentIndex == 0 {
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: 37.411822, longitude: -121.941125), withinMiles: 10)
query.limit = 200;
query.orderByDescending("createdAt")
}
if controller.selectedSegmentIndex == 1 {
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: 37.411822, longitude: -121.941125), withinMiles: 10)
query.limit = 200;
query.orderByDescending("commentsNo")
}
}
return query
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
cell.yakText.text = object.valueForKey("text") as String
cell.yakText.numberOfLines = 0
var thumbnail = object["image"] as PFFile
cell.thumb.file = thumbnail
cell.thumb.loadInBackground()
var dateCreated = object.createdAt as NSDate
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "EEE, MMM d, h:mm a"
cell.time.text = NSString(format: "%@", dateFormat.stringFromDate(dateCreated))
let replycnt = object.objectForKey("replies") as Int
let numberOfReplies = comments?.count as Int!
cell.replies.text = "\(numberOfReplies) Comments"
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "yakDetail"){
let indexPath = self.tableView.indexPathForSelectedRow()
let obj = self.objects[indexPath!.row] as PFObject
let navVC = segue.destinationViewController as UINavigationController
let detailVC = navVC.topViewController as DetailViewController
detailVC.yak = obj
}
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
}
答案 0 :(得分:1)
试试这个,如果它对你有用:
在viewDidLoad-Method
上方添加以下方法func refresh(sender:AnyObject)
{
// Updating your data here...
self.tableView.reloadData()
self.refreshControl?.endRefreshing()
}
答案 1 :(得分:0)
如果你让Parse管理你看起来像的查询,那么你需要调用loadObjects()
告诉解析运行查询并填充表。
override func viewDidAppear(animated: Bool) {
self.loadObjects()
}
内部在完成填充tableView.reloadData()
后调用self.objects
。简单来说,不是你需要担心的事情,但是要知道你是否想要自己实现它(如果你想要对这个过程有更多的控制,你可能希望以后再做)。