ios - 除非您拉动刷新,否则表中的数据不会更新。 swift ios

标签 ios uitableview swift parse-platform xcode6

我的代码中的数据表不会更新,除非你拉刷新。我需要在 View 加载时立即更新表格 View 。我一直坚持这一点,而且每当我更改分段 Controller 上的选择时,我都需要更新数据,而不必在选择特定分段以获取新数据后总是拉动刷新。以下是我的 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()

  }
 }

最佳答案

试试这个,如果它对你有用的话:

在viewDidLoad-Method上面添加如下方法

func refresh(sender:AnyObject)
{
    // Updating your data here...

    self.tableView.reloadData()
    self.refreshControl?.endRefreshing()
}

关于ios - 除非您拉动刷新,否则表中的数据不会更新。 swift ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284604/

相关文章:

ios - 使用 Switch on UITableViewCell 更改数据

ios - iOS7中的UIAlertView addSubview

ios - 通过 userDefaults 在两个 View Controller 之间共享数据

iphone - 在我的 View 中获取分组的表格 View 背景颜色

swift - 将 unicode 转换为表情符号

swift - 在 Swift 3 函数中同时运行代码块,然后等待完成

android - Sinch 像 viber 一样直接在本地通知上点击接听电话

iOS:目前有没有办法防止两个 View Controller 同时被推送或弹出?

ios - 无法获取 tableView 中的单元格数量

ios - 使用 let buttonPosition = sender.convertPoint(CGPointZero, toView : self. tableView) 点击时更改 TableViewCell 中 UIButton 的标题