Techitout
Techitout

Reputation: 37

Trying to add pull to refresh in Swift 4 but getting an error

I need help fixing this issue! I am adding pull to refresh to my table view. When I pull to refresh I want the articles to refresh. If someone who knows this would help me would be great, and if there is anything else wrong too.

Argument of '#selector' does not refer to an '@objc' method, property, or initializer

The error is here:

refreshControl.addTarget(self, action: #selector(fetchArticles(fromSource: source)), for: .valueChanged)
    return refreshControl
}()

Here is the function fetchArticles:

@objc func fetchArticles(fromSource provider: String){
    ...
}

Upvotes: 1

Views: 3564

Answers (1)

AamirR
AamirR

Reputation: 12198

You have to add another function to refresh articles, because you are passing a parameter:

@objc func refreshArticles() {
    self.fetchArticles(fromSource: self.source))
}

And addTarget like this:

refreshControl.addTarget(self, action: #selector(refreshArticles), for: .valueChanged)

Also, Do not miss to add refreshControl to tableView, as such:

tableView.addSubview(refreshControl)

Update Add following line once fetchArticles completes, as such:

refreshControl.endRefreshing()

You are using session.dataTask so this must go inside:

DispatchQueue.main.async {
    refreshControl.endRefreshing()
}

Upvotes: 1

Related Questions