gpt4 book ai didi

swift - 解析 : loadObjects/refreshing the tableview after a pull to refresh

转载 作者:行者123 更新时间:2023-11-28 08:50:09 24 4
gpt4 key购买 nike

swift

我正在努力做到这一点,当您在 tableview 上拉动刷新时,它会使用存储在 Parse.com 上的数据更新 tableview

我已经研究过了,看来我需要使用 loadObjects()

当我把它放在里面时,它说:“使用未解析的标识符‘loadObjects’

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

这是我的 ViewController.swift

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.
}
*/

}

我试过让 do_table_refresh() 函数在刷新结束后运行,但 tableview 没有更新。我也尝试过使用 self.tableview.reloadData() 但也不起作用。

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

如有任何其他信息,请随时向我询问!

感谢您的宝贵时间!

最佳答案

因此,如果您在每次拉动刷新时都尝试获取 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()

关于swift - 解析 : loadObjects/refreshing the tableview after a pull to refresh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com