How to implement pagination in tableview cell

Screen Shot 2020-09-19 at 3.17.11 PM
My app retrieves json from the newsAPI.com . When I look at the json, it shows 2000 articles however the image show 20 values loaded into my tableview controller. How do I increase the value to implement pagination?

class LatestNewsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let newsData = Articles() //Model object

let urlRequest = "https://newsapi.org/v2/everything?q=coronavirus&apiKey=d32071cd286c4f6b9c689527fc195b03" //Website API
var urlSelected = ""
var articles: [Articles]? = [] // holds array of Articles data
var indexOfPageToRequest = 1
@IBOutlet weak var table_view: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    table_view.cellLayoutMarginsFollowReadableWidth = true
    navigationController?.navigationBar.prefersLargeTitles = true
    retriveData(n: indexOfPageToRequest)
    
    
}

func retriveData( n : Int)->Int{
    guard let aritcleUrl = URL(string: urlRequest) else { //send a request to the server
        return n
    }
    
    let request = URLRequest(url: aritcleUrl)
    let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in //collects content from website
        
        if  error != nil { // checks if content is available
            print(error ?? 0)
            return
        }
        if let data = data { // converts data to an array of Article objects
            self.articles = self.parseData(data: data)
            
            
            
        }
        
        
    })
    
    task.resume()
    return n
}


func parseData(data:Data)-> [Articles]   {
    var articles: [Articles]? = [] // holds parsed data
    
    do {
        let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
        
        let jsonArticles = jsonResult?["articles"] as? [AnyObject] ?? [] // gets first head of json file and converts it to dictionary
        
        for jsonArticle in jsonArticles{ // captures data and stores it in the model object
            let article = Articles()
            article.author = jsonArticle["author"] as? String
            article.title = jsonArticle["description"] as? String
            article.publishedAt = jsonArticle["publishedAt"] as? String
            article.urlImage = jsonArticle["urlToImage"] as? String
            article.urlWebsite = jsonArticle["url"] as? String
            articles?.append(article) //put article data in the array
        }
        
        print(jsonArticles)
        
        DispatchQueue.main.async {
            
            if(articles!.count > 0)
            {
                self.table_view.reloadData()
            }
        }
        
    } catch {
        print("Nothing my guy\(error)")
    }
    
    return articles ??  [] // returns an array of articles
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 163
}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return articles?.count ?? 0
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! NewsTableViewCell
    cell.authorName.text = articles?[indexPath.row].author
    cell.headLine.text = articles?[indexPath.row].title
    cell.newsImage.downloadImage(from:(self.articles?[indexPath.item].urlImage ?? "nill"))
    cell.timePublication.text = articles?[indexPath.row].publishedAt

    
    

    return cell
}

This question seems off-topic for this forum. If it's about UIKit, I'd suggest that you ask it over https://developer.apple.com/forums/ instead. If it's about newsapi.org APIs, maybe you can check its documentation and/or see if there are support contacts or forums from their websites.

PS:

This address

let urlRequest = "https://newsapi.org/v2/everything?q=coronavirus&apiKey=d32071cd286c4f6b9c689527fc195b03"

returns 20 articles when I queried.

Terms of Service

Privacy Policy

Cookie Policy