解析:在拉动刷新后loadObjects /刷新tableview

时间:2015-12-23 23:45:05

标签: swift parse-platform pull-to-refresh

夫特

我正试图这样做,当你在tableview上刷新时它会用Parse.com上存储的数据更新tableview

我研究了这个,似乎我需要使用 @media only screen and (min-width:320px) { .start-today { color:#fffeaf; font-size: 14px; font-weight: bold; letter-spacing: 0.3px; text-align: center; position: absolute; bottom:100px; left:2px; } .start-today a { font-size:12px; } }

当我把它放入其中时说:“使用未解析的标识符'loadObjects'

不确定这是否是我需要的......

这是我的ViewController.swift

loadObjects()

我尝试在刷新结束后运行import UIKit import Parse import Bolts import ParseUI class EventsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var timer: NSTimer! var isAnimating = false var currentColorIndex = 0 var currentLabelIndex = 0 var customView: UIView! var labelsArray: Array<UILabel> = [] var refreshControl: UIRefreshControl! var testArray = [String]() var subArray = [String]() @IBOutlet weak var tableview: UITableView! @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() tableview.delegate = self tableview.dataSource = self refreshControl = UIRefreshControl() tableview.addSubview(refreshControl) loadCustomRefreshContents() //refreshControl colors refreshControl.backgroundColor = UIColor.clearColor() //color of background refreshControl.tintColor = UIColor.clearColor() //color of indicator let query = PFQuery(className: "events") let runkey = query.orderByDescending("eventTitle") runkey.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error:NSError?) -> Void in if error == nil { if let objects = objects as [PFObject]! { for object in objects { let load = object.objectForKey("eventTitle") as! String self.testArray.append(load) let subload = object.objectForKey("date") as! String self.subArray.append(subload) print(self.testArray) } } } else { print("error:\(error!) \(error!.userInfo)") } } sleep(3) do_table_refresh() loadViewIfNeeded() loadObjects() /*let url = NSURL(string: "https://m.facebook.com/CathedralCityMunicipal") let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in if error == nil { let urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding) print(urlContent) dispatch_async(dispatch_get_main_queue()) { self.webView.loadHTMLString(urlContent! as String, baseURL: nil) } } } task.resume()*/ // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func do_table_refresh() { dispatch_async(dispatch_get_main_queue()) { self.tableview.reloadData() return } } func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { return testArray.count } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("eventcell", forIndexPath: indexPath) as! EventTableViewCell cell.title.text = self.testArray[indexPath.row] cell.subTitle.text = self.subArray[indexPath.row] return cell } //refreshes tableview; starts refresh func loadCustomRefreshContents() { let refreshContents = NSBundle.mainBundle().loadNibNamed("RefreshControl", owner: self, options: nil) customView = refreshContents[0] as! UIView customView.frame = refreshControl.bounds for var i=0; i<customView.subviews.count; ++i { labelsArray.append(customView.viewWithTag(i + 1) as! UILabel) } refreshControl.addSubview(customView) do_table_refresh() } //stops refresh func scrollViewDidEndDecelerating(scrollView: UIScrollView) { do_table_refresh() if refreshControl.refreshing { if !isAnimating { doSomething() animateRefreshStep1() self.do_table_refresh() } } } //cycles through colors func getNextColor() -> UIColor { var colorsArray: Array<UIColor> = [UIColor.magentaColor(), UIColor.brownColor(), UIColor.yellowColor(), UIColor.redColor(), UIColor.greenColor(), UIColor.blueColor(), UIColor.orangeColor()] if currentColorIndex == colorsArray.count { currentColorIndex = 0 } let returnColor = colorsArray[currentColorIndex] ++currentColorIndex return returnColor } func doSomething() { timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "endOfWork", userInfo: nil, repeats: true) self.do_table_refresh() } func endOfWork() { refreshControl.endRefreshing() self.do_table_refresh() timer.invalidate() timer = nil } //first part of animation func animateRefreshStep1() { isAnimating = true UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in self.labelsArray[self.currentLabelIndex].transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4)) self.labelsArray[self.currentLabelIndex].textColor = self.getNextColor() }, completion: { (finished) -> Void in UIView.animateWithDuration(0.05, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in self.labelsArray[self.currentLabelIndex].transform = CGAffineTransformIdentity self.labelsArray[self.currentLabelIndex].textColor = UIColor.blackColor() }, completion: { (finished) -> Void in ++self.currentLabelIndex if self.currentLabelIndex < self.labelsArray.count { self.animateRefreshStep1() } else { self.animateRefreshStep2() } }) }) } //second part of animation func animateRefreshStep2() { UIView.animateWithDuration(0.35, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in self.labelsArray[0].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[1].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[2].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[3].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[4].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[5].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[6].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[7].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[8].transform = CGAffineTransformMakeScale(1.5, 1.5) self.labelsArray[9].transform = CGAffineTransformMakeScale(1.5, 1.5) }, completion: { (finished) -> Void in UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in self.labelsArray[0].transform = CGAffineTransformIdentity self.labelsArray[1].transform = CGAffineTransformIdentity self.labelsArray[2].transform = CGAffineTransformIdentity self.labelsArray[3].transform = CGAffineTransformIdentity self.labelsArray[4].transform = CGAffineTransformIdentity self.labelsArray[5].transform = CGAffineTransformIdentity self.labelsArray[6].transform = CGAffineTransformIdentity self.labelsArray[7].transform = CGAffineTransformIdentity self.labelsArray[8].transform = CGAffineTransformIdentity self.labelsArray[9].transform = CGAffineTransformIdentity }, completion: { (finished) -> Void in if self.refreshControl.refreshing { self.currentLabelIndex = 0 self.animateRefreshStep1() } else { self.isAnimating = false self.currentLabelIndex = 0 for var i=0; i<self.labelsArray.count; ++i { self.labelsArray[i].textColor = UIColor.blackColor() self.labelsArray[i].transform = CGAffineTransformIdentity } } }) }) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } 函数,但tableview不会更新。我也尝试使用do_table_refresh(),但也无效。

我不确定我做错了什么,非常感谢任何帮助!

随时向我询问任何其他信息!

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

因此,如果您每次进行即时刷新时都尝试获取Parse数据,则应始终查询它们。

将查询保存到单个函数中并调用tableView.reloadData(),以便每次查询事件时都会重新加载tableView:

func loadEvents () -> Void {

        self.testArray = []
        self.subArray = []

        let query = PFQuery(className: "events")
        let runkey = query.orderByDescending("eventTitle")
        runkey.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error:NSError?) -> Void in

            if error == nil {


                if let objects = objects as [PFObject]! {
                    for object in objects {

                        let load = object.objectForKey("eventTitle") as! String
                        self.testArray.append(load)

                        let subload = object.objectForKey("date") as! String
                        self.subArray.append(subload)

                        //reload TableView
                        self.tableView.reloadData()
                        print(self.testArray)
                    }

                }
            } else {

                print("error:\(error!) \(error!.userInfo)")
            }

        }

        refreshControl.endRefreshing()
}

然后在ViewDidLoad()

中为您的UIRefreshControl添加一个Target
 refreshControl.addTarget(self, action: Selector("loadEvents"), forControlEvents: UIControlEvents.ValueChanged)
 self.tableView.addSubview(refreshControl)

此外,您必须按如下方式声明您的UIRefreshControl:

var refreshControl = UIRefreshControl()