I am building an RSS reader using swift and need to implement pull to reload functionality.
Here is how i am trying to do it.
class FirstViewController: UIViewController,
UITableViewDelegate, UITableViewDataSource {
@IBOutlet var refresh: UIScreenEdgePanGestureRecognizer
@IBOutlet var newsCollect: UITableView
var activityIndicator:UIActivityIndicatorView? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.newsCollect.scrollEnabled = true
// Do any additional setup after loading the view, typically from a nib.
if nCollect.news.count <= 2{
self.collectNews()
}
else{
self.removeActivityIndicator()
}
view.addGestureRecognizer(refresh)
}
@IBAction func reload(sender: UIScreenEdgePanGestureRecognizer) {
nCollect.news = News[]()
return newsCollect.reloadData()
}
I am getting :
Property 'self.refresh' not initialized at super.init call
Please help me to understand the behaviour of Gesture recognisers. A working sample code will be a great help.
Thanks.
Swift 4
And you could stop refreshing with:
What the error is telling you, is that
refresh
isn't initialized. Note that you chose to makerefresh
not optional, which in Swift means that it has to have a value before you callsuper.init
(or it's implicitly called, which seems to be your case). Either makerefresh
optional (probably what you want) or initialize it in some way.I would suggest reading the Swift introductory documentation again, which covers this in great length.
One last thing, not part of the answer, as pointed out by @Anil, there is a built in pull to refresh control in iOS called
UIRefresControl
, which might be something worth looking into.I built a RSS feed app in which I have a Pull To refresh feature that originally had some of the problems listed above.
But to add to the users answers above, I was looking everywhere for my use case and could not find it. I was downloading data from the web (RSSFeed) and I wanted to pull down on my tableView of stories to refresh.
What is mentioned above cover the right areas but with some of the problems people are having, here is what I did and it works a treat:
I took @Blankarsch 's approach and went to my main.storyboard and select the table view to use refresh, then what wasn't mentioned is creating IBOutlet and IBAction to use the refresh efficiently
Hope this helps anyone in same situation as me
func pullToRefresh(){
This is how I made it work using Xcode 7.2 which I think is a major bug. I'm using it inside my
UITableViewController
inside myviewWillAppear
As you can see, I literally have to call the
configureMessage()
method after setting up myUIRefreshControl
then after that, subsequent refreshes will work fine.Pull to refresh is built in iOS. You could do this in swift like
At some point you could end refreshing.