Reputation: 33
I have successfully parsed and decoded the JSON data and it displays in the console. I don't know how I would set up pagination with the api and display all of the data when I scroll down on the tableView. The API I'm using is https://rickandmortyapi.com/api/character/. Thank you for all your help in advance!
This is the code in my main ViewController
import UIKit
struct PagedCharacters: Codable {
struct Info: Codable {
let count: Int
let pages: Int
let next: String
let prev: String
}
struct Results: Codable {
let name: String
let status: String
let species: String
}
let info: Info
let results: [Results]
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var uiTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getRickAndMortyData()
self.uiTableView.dataSource = self
self.uiTableView.delegate = self
// Do any additional setup after loading the view.
}
func getRickAndMortyData() {
//create instance of the url
let url = "https://rickandmortyapi.com/api/character/"
//construct the url, use guard to avoid nonoptional
guard let urlObj = URL(string: url) else
{ return }
//fetch data
URLSession.shared.dataTask(with: urlObj) {(data, response, error) in
//to avoid non optional in JSONDecoder
guard let data = data else { return }
do {
//decode object
let downloadedRickAndMorty = try JSONDecoder().decode(PagedCharacters.self, from: data)
DispatchQueue.main.async {
self.uiTableView.reloadData()
}
print(downloadedRickAndMorty)
} catch {
print(error)
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "rickandmortyCell") as? CharacterTableViewCell else { return UITableViewCell() }
cell.nameLabel.text = "Name: " + PagedCharacters.Results[indexPath.row].name
return cell
}
}
This is my tableviewcell class
import UIKit
class CharacterTableViewCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
This is the data I'm getting in my console when the parsed data is printed. I'm still not getting all the data from the different pages.
PagedCharacters(info: API_Practice.PagedCharacters.Info(count: 493, pages: 25, next: "https://rickandmortyapi.com/api/character/?page=2", prev: ""), results: [API_Practice.PagedCharacters.Results(name: "Rick Sanchez", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Morty Smith", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Summer Smith", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Beth Smith", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Jerry Smith", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Abadango Cluster Princess", status: "Alive", species: "Alien"), API_Practice.PagedCharacters.Results(name: "Abradolf Lincler", status: "unknown", species: "Human"), API_Practice.PagedCharacters.Results(name: "Adjudicator Rick", status: "Dead", species: "Human"), API_Practice.PagedCharacters.Results(name: "Agency Director", status: "Dead", species: "Human"), API_Practice.PagedCharacters.Results(name: "Alan Rails", status: "Dead", species: "Human"), API_Practice.PagedCharacters.Results(name: "Albert Einstein", status: "Dead", species: "Human"), API_Practice.PagedCharacters.Results(name: "Alexander", status: "Dead", species: "Human"), API_Practice.PagedCharacters.Results(name: "Alien Googah", status: "unknown", species: "Alien"), API_Practice.PagedCharacters.Results(name: "Alien Morty", status: "unknown", species: "Alien"), API_Practice.PagedCharacters.Results(name: "Alien Rick", status: "unknown", species: "Alien"), API_Practice.PagedCharacters.Results(name: "Amish Cyborg", status: "Dead", species: "Alien"), API_Practice.PagedCharacters.Results(name: "Annie", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Antenna Morty", status: "Alive", species: "Human"), API_Practice.PagedCharacters.Results(name: "Antenna Rick", status: "unknown", species: "Human"), API_Practice.PagedCharacters.Results(name: "Ants in my Eyes Johnson", status: "unknown", species: "Human")])
If you need anything else please let me know. Thank you!
Upvotes: 3
Views: 13064
Reputation: 1177
There were many flaws in your logic, so I have rewritten it with minimal changes. Now it will serve your purpose.
import UIKit
struct PagedCharacters: Codable {
let info: Info
let results: [Results]
}
struct Info: Codable {
let count: Int
let pages: Int
let next: String
let prev: String
}
struct Results: Codable {
let name: String
let status: String
let species: String
}
And here is the code for view controller class:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var uiTableView: UITableView!
var aryDownloadedData:[Results]?
var nextPageUrl:String!
override func viewDidLoad() {
super.viewDidLoad()
getIntitalRickAndMortyData()
self.uiTableView.dataSource = self
self.uiTableView.delegate = self
// Do any additional setup after loading the view.
}
func getIntitalRickAndMortyData(){
aryDownloadedData = []
//here first page is next page
nextPageUrl = "https://rickandmortyapi.com/api/character/"
getRickAndMortyData()
}
func getRickAndMortyData() {
//construct the url, use guard to avoid nonoptional
guard let urlObj = URL(string: nextPageUrl) else
{ return }
//fetch data
URLSession.shared.dataTask(with: urlObj) {[weak self](data, response, error) in
//to avoid non optional in JSONDecoder
guard let data = data else { return }
do {
//decode object
let downloadedRickAndMorty = try JSONDecoder().decode(PagedCharacters.self, from: data)
self?.aryDownloadedData?.append(contentsOf: downloadedRickAndMorty.results)
self?.nextPageUrl = downloadedRickAndMorty.info.next
DispatchQueue.main.async {
self?.uiTableView.reloadData()
}
print(self?.aryDownloadedData as Any)
} catch {
print(error)
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.aryDownloadedData?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let count = aryDownloadedData?.count, count>1{
let lastElement = count - 1
if indexPath.row == lastElement {
//call get api for next page
getRickAndMortyData()
}
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: "rickandmortyCell") as? CharacterTableViewCell else { return UITableViewCell() }
cell.nameLabel.text = "Name: " + (self.aryDownloadedData?[indexPath.row].name ?? "default")
return cell
}
}
Upvotes: 3