JSON data not passing to tableView Cell - json

I am trying to get the below code to pass JSON data to the table viewCell. I have confirmed that the JSON data is being captured and stored in the variable downloadLenderRates. But I cannot get the values to pass to the tabelView Cell. I confirmed that the cell identifier is named correctly and the swift file that helps manage the tableView cell is named correctly. At this point, I get no error messages and just a blank table when I run the app. I am not sure why!
class MortgageRatesVC: UIViewController, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let mortgousURL = URL(string:"http://mortgous.com/JSON/currentRatesJSON.php")!
var lenderRates = [LenderRate]()
override func viewDidLoad() {
super.viewDidLoad()
downloadJason()
}
func downloadJason () {
lenderRates = []
// guard let downloadURL = url else { return }
URLSession.shared.dataTask(with: mortgousURL) { data, urlResponse, error in
guard let data = data else { return }
do {
let dateFormat = DateFormatter()
dateFormat.locale = Locale(identifier: "en_US_POSIX")
dateFormat.dateFormat = "yyyy-MM-dd"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormat)
let downloadLenderRates = try decoder.decode([LenderRate].self, from: data)
// print(downloadLenderRates)
self.lenderRates = downloadLenderRates
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print(error)
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lenderRates.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "LenderCell") as? LenderCell else { return UITableViewCell() }
cell.lenderNamelbl.text = lenderRates[indexPath.row].financialInstitution
print(lenderRates[indexPath.row].financialInstitution)
return cell
}
}

The syntax
guard let cell = tableView.dequeueReusableCell(withIdentifier: "LenderCell") as? LenderCell else {
return UITableViewCell()
}
is very bad habit.
The guard can only fail if there is a design error which occurs for example if the developer forgot to set the class of the cell to the custom class. In this case you won't see anything in the table view.
This is one of the few cases where force-unwrapping is recommended. If the design is set up properly the cell is valid and its type is the custom class. Further use always the API dequeueReusableCell(withIdentifier:for:) which returns a non-optional cell.
let cell = tableView.dequeueReusableCell(withIdentifier: "LenderCell", for: indexPath) as! LenderCell

Related

Table empty when populating cells from json

I am trying to populate a table with json content. Everything seems to work fine except that the table is not showing any data. Actually, the code shown below should display the "title" information of each json data array into one cell. See line
cell.textLabel?.text = myNewsItems[indexPath.row].title
However, from what I can see in the console output, I can verify that the news array is parsed like expected (see Checkpoint: print(myNewsS)).
Any idea what I am missing?
Swift4
import UIKit
// structure from json file
struct News: Codable{
let type: String
let timestamp: String
let title: String
let message: String
}
class HomeVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
var myTableView = UITableView()
var myNewsItems: [News] = []
override func viewDidLoad() {
super.viewDidLoad()
let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
myTableView = UITableView(frame: CGRect(x: 0, y: 150, width: displayWidth, height: displayHeight - barHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
// JSON
let url=URL(string:"https://api.myjson.com/bins/ywv0k")
let session = URLSession.shared
let task = session.dataTask(with: url!) { (data, response, error) in
// check status 200 OK etc.
guard let data = data else { return }
do {
let myNewsS = try
JSONDecoder().decode([News].self, from: data)
print(myNewsS)
DispatchQueue.main.async {
self.myTableView.reloadData()
}
} catch let jsonErr {
print("Error json:", jsonErr)
}
}
task.resume()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myNewsItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = myNewsItems[indexPath.row].title
return cell
}
}
Assign the array
myNewsItems = myNewsS
DispatchQueue.main.async {
self.myTableView.reloadData()
}

Swift 4, Json data doesn't appear in cells

I can't put data to the cells, I searched in different tutorials and it should work , I checked in debug area and data are downloaded but doesn't exist in cells, I tried also with custom cells but it doesn't work too. I have not got any error message, simply empty cells. Do you know maybe what can cause this issue? I spended much time for searching solution but I can't find anything, on every tutorial people do this similar to me.
struct Country: Decodable {
let name: String
}
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var countries = [Country]()
var liczba = Int()
#IBOutlet var tv: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
json() {
self.tv.reloadData()
}
tv.dataSource = self
tv.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return liczba
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = countries[indexPath.row].name
return cell
}
func json (completed: #escaping()->()) {
let jsonUrl = "https://restcountries.eu/rest/v2/all"
let url = URL(string: jsonUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
self.countries = try JSONDecoder().decode([Country].self, from: data!)
let numer = self.countries.count
self.liczba = numer
}
catch {
print("error")
}
}.resume()
}
}
It seems that you are having a problem with the sequentiality of your code. You are calling reloadData on your tableview but at that moment you don't have set the datasource yet.
try this:
tv.dataSource = self
tv.delegate = self
json() {
self.tv.reloadData()
}
You are never calling the completed closure of your json method, so self.tv.reloadData() will never be executed.
Add completed() after self.liczba = numer.
Since you are not returning anything from the closure I recommend to delete the completion handler and reload the table view on the main thread within the closure.
And don't forget to handle a potential error
func json() {
let jsonUrl = "https://restcountries.eu/rest/v2/all"
let url = URL(string: jsonUrl)
URLSession.shared.dataTask(with: url!) { [unowned self] (data, response, error) in
if let error = error { print(error); return }
do {
self.countries = try JSONDecoder().decode([Country].self, from: data!)
self.liczba = self.countries.count
DispatchQueue.main.async {
self.tv.reloadData()
}
}
catch {
print("error")
}
}.resume()
}
and call the method in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tv.dataSource = self // I'd connect datasource and delegate in Interface Builder
tv.delegate = self
json()
}

data is not filtered by the searchbar

I am trying to filter my fetched JSON data using a searchbar. However, when I type something into the searchbar it does nothing. Data is still in the same place and it is not filtered, whereas it should be dynamically filtered while I am typing something into the searchbar.
The code below shows my TableViewController as well as the function for fetching JSON data into my array. It is then filtered using a searchbar and whenever the data's name is matching the condition in the search bar it is then added to the second array called 'filteredExercise'.
import UIKit
class ExerciseTableViewController: UITableViewController, UISearchBarDelegate {
var fetchedExercise = [Exercise]()
var filteredExercise = [Exercise]()
var inSearchMode = false
#IBOutlet var searchBar: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
parseData()
}
func parseData() {
fetchedExercise.removeAll()
let urlPath = "https://wger.de/api/v2/exercise/?format=json&language=2&status=2&limit=200"
let url = URL(string: urlPath)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Error while parsing JSON")
}
else {
do {
if let data = data,
let fetchedData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String:Any],
let exercises = fetchedData["results"] as? [[String: Any]] {
for eachExercise in exercises {
if eachExercise["license_author"] as! String == "wger.de" {
let name = eachExercise["name"] as! String
let description = eachExercise["description"] as! String
let id = eachExercise["id"] as! Int
self.fetchedExercise.append(Exercise(name: name, description: description, id: id))
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
catch {
print("Error while parsing data.")
}
}
}
task.resume()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if inSearchMode {
return filteredExercise.count
}
return fetchedExercise.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ExerciseCell", for: indexPath) as? ExerciseCell {
let exercise: Exercise!
if inSearchMode {
exercise = filteredExercise[indexPath.row]
cell.configureCell(exercise: exercise)
} else {
exercise = fetchedExercise[indexPath.row]
cell.configureCell(exercise: exercise)
}
return cell
} else {
return UITableViewCell()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var exercise: Exercise!
exercise = fetchedExercise[indexPath.row]
performSegue(withIdentifier: "exerciseDetailVC", sender: exercise)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
self.tableView.reloadData()
} else {
inSearchMode = true
let lower = searchBar.text!.lowercased()
filteredExercise = fetchedExercise.filter({$0.name.range(of: lower) != nil})
self.tableView.reloadData()
}
}
}
Looks like you have an error in?
#IBOutlet var searchBar: UITableView!
I think it should be type of UISearchBarController.
Okay, I have finally figured out what is wrong with it.
Basically first of all I had incorrect type for my searchBar due to the Xcode bug and I did not see that.
Then I had to connect my IBOutlet to the storyboard as well because it was not done.
Finally I started getting wrong results while filtering through the data and it was because I have been filtering through results using a lowercased() function, whereas all my data is capitalized.

How do I populate a tableview with JSON data from Alamofire?

Before I state my problem, I want to let everyone know that I am new to the coding environment that is Swift, so forgive me for my lack of knowledge. Currently, I am having trouble populating the cells of a tableview using Alamofire based on the data that is returned from a JSON URL. When I run the app in a simulator, data is displayed in the console, but the app crashes with a SIGABRT error. For reference, instead of using a viewcontroller with a tableview element inside, I am using a tableviewcontroller. Here is my code thus far:
import UIKit
import Alamofire
class TableViewController: UITableViewController {
var responseArray: NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("https://rss.itunes.apple.com/api/v1/us/apple-music/top-songs/all/10/explicit.json").responseJSON { response in
if let json = response.result.value {
print(json)
self.responseArray = json as! NSArray
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return responseArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "top10", for: indexPath)
// Configure the cell...
let whichSong = responseArray[(indexPath as NSIndexPath).row]
let artistName = (whichSong as AnyObject)["artistName"] as? String
cell.textLabel?.text = artistName
return cell
}
The crash occurs because the root object of the JSON is a dictionary (represented by {}) not an array.
First of all declare a type alias for a JSON dictionary and the data source array as native type, an array of JSON dictionaries:
typealias JSONDictionary = [String:Any]
var responseArray = [JSONDictionary]()
Then parse the JSON and reload the table view, you want probably the array for key results:
Alamofire.request("https://rss.itunes.apple.com/api/v1/us/apple-music/top-songs/all/10/explicit.json").responseJSON { response in
if let json = response.result.value as? JSONDictionary,
let feed = json["feed"] as? JSONDictionary,
let results = feed["results"] as? [JSONDictionary] {
print(results)
self.responseArray = results
self.tableView.reloadData()
}
}
Then show the data in cellForRow
let song = responseArray[indexPath.row]
cell.textLabel?.text = song["artistName"] as? String
Okay so firstly change
let cell = tableView.dequeueReusableCell(withIdentifier: "top10", for: indexPath)
to
let cell = tableView.dequeueReusableCell(withIdentifier: "top10")
However, with this, cell will be cell?, you will have to return cell!.
Next in your Alamofire response,
if let json = response.result.value {
print(json)
self.responseArray = json as! NSArray
self.reloadData()
//If above line doesn't work, try tableView.reloadData()
}
Why?
The Alamofire request is "asynchronous", meaning it executes codes while your app is doing other things. Therefor, it is likely that you are setting that array after your table is loaded, hence the reloadData()
Replace the below line
let cell = tableView.dequeueReusableCell(withIdentifier: "top10", for: indexPath)
with
let cell = tableView.dequeueReusableCell(withIdentifier: "top10")

Problems with adding value to an array with array.append() in Swift

in the moment we're programming a Swift App for iOS in which we want to get data of our JSON Website (MySql database) into the TableViewCell. The problem is by appending the text values of the strings for the label in the cell. Swift can import the JSON values into the name variable but I cant assign it to the text array for the cells. I havent no syntax errors, but the data[0] Variable print sth. as "123". Why it is 123? The test Value is "Test". I don't now where the problem by appending the value to the array is, that the result is 123 after that. Please help.
Here is the sourcecode:
class listViewViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var data:[String?] = []
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let myUrl = URL(string: "");//Empty link for this question
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"
let postString = "lid=1";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let Name = parseJSON["Name"] as? String
print("\(Name)")//Test
self.data.append(Name!)
print("\(data![0])" as String)//123
}
} catch {
print(error)
}
}
task.resume()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! listViewTableViewCell
print("\(data[indexPath.row])")
let dataCell = data[indexPath.row]
cell.listViewCell.text = dataCell
return cell
}
}
this because your array properties and data callback block parameter have the same name "data". in your code you user print("(data![0])" as String) instead of print("(self.data![0])" as String) => you have to add self.
then you can optimise your code like this (it's optional : it's just like a code review ;) )
try to do this
- change your array type to String like this
var data = [String]()
- webService callback change your code like this :
if let parseJSON = json {
if let Name = parseJSON["Name"] as? String{
print("\(Name)")
self.data.append(Name)
print("\(self.data.last)")//123
}
}
When you append to your data array you use self.data but you then print from data which is the parameter to the inner function. You add and print from different arrays.