Clicking Tableview Cell Does Not Display Data - json

I'm having trouble with my didSelectRowAt function of my tableview. For some reason, although my segue identifier and destination view are correct, when I click the row, it does not load anything. Furthermore, Xcode informs me:
Cannot assign value of type 'Double?' to type 'String?'
but I cannot find an online resource that tells me how to resolve this. For reference, the data I would like to display is from a nested JSON feed.
JSON Struct
struct PlayerStatsParent:Decodable{
let rankings: [PlayerStats]
}
struct PlayerStats:Decodable {
let personaname: String?
let score: Double?
let solo_competitive_rank: Int?
let avatar: String?
}
Cell Select Function
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? RankDetail {
destination.playerRank = rank[(rankTable.indexPathForSelectedRow?.row)!]
}
}
Destination View Controller Code
import UIKit
class RankDetail: UIViewController {
#IBOutlet var rankLabel: UILabel!
#IBOutlet var scoreLabel: UILabel!
var playerRank:PlayerStats?
override func viewDidLoad() {
super.viewDidLoad()
rankLabel.text = "\(playerRank?.solo_competitive_rank)"
scoreLabel.text = playerRank?.score
}

Also check your select method.Should be this.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
Not this;
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
If you select the cell.

Looks like the error occurs in the RankDetail class.
Here's a simple fix.
class RankDetail: UIViewController {
#IBOutlet var rankLabel: UILabel!
#IBOutlet var scoreLabel: UILabel!
var playerRank:PlayerStats?
override func viewDidLoad() {
super.viewDidLoad()
rankLabel.text = "\(playerRank?.solo_competitive_rank)"
// scoreLabel.text if of type String?, so it can't store Double? directly
// scoreLabel.text = playerRank?.score
// converting to String? should fix it
scoreLabel.text = "\(playerRank?.score)"
}
EDIT:
Present ViewController using the following code. Saves you the trouble of having to worry about segues.
if let viewController = storyboard?.instantiateViewController(withIdentifier: "NewViewController") {
present(viewController, animated: true, completion: nil)
}

Related

swift When I click on didselectRow I want to see the json data on the detail page image , title and year?

When I click on it, I want to show the picture title and year part in the cell section in the tableview, how can I do this? When we click on the row go detail page and big picture, title and year I want to show the big title and year. How can I populate the DetailViewController page?
ViewController
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var table: UITableView!
#IBOutlet var field: UITextField!
var movies = [Movie]()
override func viewDidLoad() {
super.viewDidLoad()
table.register(MovieTableViewCell.nib(), forCellReuseIdentifier: MovieTableViewCell.identifier)
table.delegate = self
table.dataSource = self
field.delegate = self
}
// Field
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
searchMovies()
return true
}
func searchMovies() {
field.resignFirstResponder()
guard let text = field.text, !text.isEmpty else {
return
}
let query = text.replacingOccurrences(of: " ", with: "%20")
movies.removeAll()
URLSession.shared.dataTask(with: URL(string: "https://www.omdbapi.com/?apikey=3aea79ac&s=\(query)&type=movie")!,
completionHandler: { data, response, error in
guard let data = data, error == nil else {
return
}
// Convert
var result: MovieResult?
do {
result = try JSONDecoder().decode(MovieResult.self, from: data)
}
catch {
print("error")
}
guard let finalResult = result else {
return
}
// Update our movies array
let newMovies = finalResult.Search
self.movies.append(contentsOf: newMovies)
// Refresh our table
DispatchQueue.main.async {
self.table.reloadData()
}
}).resume()
}
// Table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movies.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MovieTableViewCell.identifier, for: indexPath) as! MovieTableViewCell
cell.configure(with: movies[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Show movie details
if let vc = storyboard?.instantiateViewController(identifier: "detailViewController") as? detailViewController{
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
struct MovieResult: Codable {
let Search: [Movie]
}
struct Movie: Codable {
let Title: String
let Year: String
let imdbID: String
let _Type: String
let Poster: String
private enum CodingKeys: String, CodingKey {
case Title, Year, imdbID, _Type = "Type", Poster
}
}
MovieTableViewCell
class MovieTableViewCell: UITableViewCell {
#IBOutlet var movieTitleLabel: UILabel!
#IBOutlet var movieYearLabel: UILabel!
#IBOutlet var moviePosterImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
static let identifier = "MovieTableViewCell"
static func nib() -> UINib {
return UINib(nibName: "MovieTableViewCell",
bundle: nil)
}
func configure(with model: Movie) {
self.movieTitleLabel.text = model.Title
self.movieYearLabel.text = model.Year
let url = model.Poster
if let data = try? Data(contentsOf: URL(string: url)!) {
self.moviePosterImageView.image = UIImage(data: data)
}
}
}
DetailViewController
import UIKit
class detailViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var titleLbl: UILabel!
#IBOutlet weak var yearLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
You can pass the data there when you create your UIViewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Show movie details
if let vc = storyboard?.instantiateViewController(identifier: "detailViewController") as? detailViewController{
vc.item = movies[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
}
But you need to pass data first, and when you controller initialized (loaded) you can assign value to loaded views
class detailViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var titleLbl: UILabel!
#IBOutlet weak var yearLbl: UILabel!
var item: Movie? = nil
override func viewDidLoad() {
super.viewDidLoad()
if (let movie = item) {
titleLbl.text = item.title
yearLbl.tex = item.Year
// also image
}
}
}

Passing JSON to another view controller

I'm having a hard time trying to pass JSON to another view controller using a segue. So for I have only been able to use prepare(for segue, sender), but I can't get my data to populate my outlets on my view controller. Below is my first view controller. Within the prepare(for segue, sender) method you can see my commented out code that's not working. Any advice?
class ViewController2 : UIViewController, UITableViewDelegate, UITableViewDataSource {
var pictures : [Hit] = []
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
NetworkMananger.shared.getInfo { [weak self] (results) in
guard let self = self else {return }
switch results {
case .failure(let error):
print(error)
case .success(let pictures):
self.pictures = pictures
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ViewController1"{
if let indexPath = self.tableView.indexPathForSelectedRow{
let vc = segue.destination as! ViewController1
// vc.downloadLabel = String(pictures[indexPath.row].downloads)
// vc.tagsLabel = pictures[indexPath.row].tags
// vc.imageData = UIImage(named: pictures[indexPath.row].previewURL)
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(identifier: "ViewController1") as? ViewController1
navigationController?.pushViewController(vc!, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell123", for: indexPath) as! firstTableViewCell
cell.label?.text = String(pictures[indexPath.row].downloads)
cell.downloadPictureFromURL(from: pictures[indexPath.row].previewURL)
return cell
}
}
Below is my second view controller I want to pass my JSON to :
class ViewController1: UIViewController {
var picture : Hit!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var imageData:
UIImageView!
#IBOutlet weak var tagsLabel: UILabel!
#IBOutlet weak var downloadLabel: UILabel!
var cap : String = ""
override func viewDidLoad() {
super.viewDidLoad()
}
}
JSON
struct Response : Codable {
let hits : [Hit]
}
struct Hit : Codable {
let tags : String
let previewURL : String
let downloads : Int
}
There are a couple of things getting in your way:
Right now, in your commented code, you have a type mismatch with each property you're trying to set. For example, you're trying to set a String to downloadLabel, which is a UILabel.
At the time of prepareForSegue, the IBOutlets may not be loaded.
To get around these issues, you could set properties on ViewController1 and then initialize the views in viewDidLoad:
struct ViewController1Input {
var downloadLabelText : String
var tagsLabelText: String
var imageName: String
}
class ViewController1: UIViewController {
var picture : Hit! //careful here -- I'm not sure where this is coming from and you aren't setting it in your segue
var input : ViewController1Input?
#IBOutlet weak var label: UILabel!
#IBOutlet weak var imageData: UIImageView!
#IBOutlet weak var tagsLabel: UILabel!
#IBOutlet weak var downloadLabel: UILabel!
var cap : String = ""
override func viewDidLoad() {
super.viewDidLoad()
if let input = input {
downloadLabel.text = input.downloadLabelText
tagsLabel.text = input.tagsLabelText
imageData.image = UIImage(named: input.imageName)
}
}
}
And in your segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ViewController1"{
if let indexPath = self.tableView.indexPathForSelectedRow{
let vc = segue.destination as! ViewController1
vc.input = ViewController1Input(downloadLabelText: String(pictures[indexPath.row].downloads),
tagsLabelText: pictures[indexPath.row].tags,
imageName: pictures[indexPath.row].previewURL)
}
}
}

I can't print to TableView data with Swift JSON

I'm new to using JSON and wanted to start with a simple app to provide an overview of the movie. The following code does not print anything on the tableView, the app remains empty, with no results. He makes no mistakes. In the debug area, however, the data prints them to me. How can I get the results on the tableView?
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var popularMoviesArray = [Results]()
var swiftManager = SwiftManager()
var tableViewCell = TableViewCell()
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var labelError: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
swiftManager.delegate = self
swiftManager.fetchUrl()
self.tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return popularMoviesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
let item = popularMoviesArray[indexPath.row]
cell.labelTitle.text = item.title
cell.labelYear.text = item.release_date
cell.labelRate.text = String(item.vote_average ?? 0.0)
cell.labelOreview.text = item.overview
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToDetail", sender: indexPath.row)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier != nil else {
return
}
let letRow = sender as? Int
switch segue.identifier {
case "goToDetail":
(segue.destination as! ViewControllerDetail).itemDetail = popularMoviesArray[letRow!]
default:
return
}
}
}
extension ViewController: SwiftManagerDelegate {
func didUpdateStruct(_ swiftManager: SwiftManager, swiftData: SwiftData) {
DispatchQueue.main.async {
self.popularMoviesArray = swiftData.results
print("PRINT ARRAY - \(self.popularMoviesArray)")
}
}
func didFailWithError(error: Error) {
print(error)
}
}
You have to reload the table view in the delegate method because the data is loaded asynchronously
func didUpdateStruct(_ swiftManager: SwiftManager, swiftData: SwiftData) {
DispatchQueue.main.async {
self.popularMoviesArray = swiftData.results
self.tableView.reloadData()
print("PRINT ARRAY - \(self.popularMoviesArray)")
}
}
Reloading the table view in viewDidLoad is pointless.

Loading tableView after JSON only

I realize the tableView.reloadData() get's called in the JSON completion block to reload the tableView with the data received; I was wondering if there was a way to load the tableView only after this completion block has finished. What is happening is that the tableView first loads empty with default cells and a few seconds later the reloadData() method gets called inside the completion block and the tableView reloads and the data appears with the custom cells. I want to load the tableView ONLY when and after the data is received. What approach can I take? Is there a way to load the view only after this is completed? I basically don't want to have the user look at a blank table for a few seconds and wait for the data to appear. Here is my viewController code and simple structs to hold the model for the data.
viewController:
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var users = [User]()
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
guard let url = URL(string: "https://jsonplaceholder.typicode.com/users") else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
guard let data = data else { return }
let recieved = try JSONDecoder().decode([User].self, from: data)
self.users = recieved
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let error as NSError {
print("Error: \(error.description)")
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.name.text = users[indexPath.row].name
cell.eMail.text = users[indexPath.row].email
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
Structs:
struct User: Decodable {
let id: Int
let name: String
let username: String
let email: String
let company: Company
}
struct Company: Decodable {
let name: String
let catchPhrase: String
let bs: String
}
Since you're essentially waiting for a network call before the data can be displayed, why not display a spinner or activity indicator on a view on top of the tableview then dismiss this when the data has been parsed successfully (or handle any errors). The alternative could be to request the data before the view is loaded in another class.
I think you can add activity indicator in your UITableView. So User will not see only blank UITableView. Or you can add background image in your UITableView, You can show it if the data is still empty and hide it after JSON decoded.
for reference background image in UITableView, you can see here
As per your suggestions; here is the route I took using an activityIndicator. I set a UIView onto of the tableView, then added an activityIndicator on top of that UIView and I also added a simple UILabel next to the activityIndicator with the string "Loading". I used propertyAnimator inside the JSON task after data had been received and after reloading the tableView, then stopping activityIndicator, fading out the UIView to show the tableView and then removing the UIView from the superView. Here is the code:
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var users = [User]()
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var loadingView: UIView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "https://jsonplaceholder.typicode.com/users") else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
guard let data = data else { return }
let recievedUsers = try JSONDecoder().decode([User].self, from: data)
self.users = recievedUsers
DispatchQueue.main.async {
self.tableView.reloadData()
if self.loadingView.alpha == 1.0 {
self.activityIndicator.stopAnimating()
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0,
delay: 0.0,
options: [],
animations: {
self.loadingView.alpha = 0.0
},
completion: { (position) in
if position == .end {
self.loadingView.removeFromSuperview()
}
})
}
}
} catch let error as NSError {
print("Error: \(error.description)")
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.name.text = users[indexPath.row].name
cell.eMail.text = users[indexPath.row].email
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}

data passing and using json data from alamofire with swift

I integrated alamofire, but I got one problem to use data within server communication.
Before I tell about my problem I will show my code:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var MainViewTable: UITableView!
#IBOutlet weak var SidesOpen: UIBarButtonItem!
#IBOutlet weak var GroupButton: UIButton!
var apps:[sample] = [sample]()
override func viewDidLoad() {
super.viewDidLoad()
SidesOpen.target = self.revealViewController()
SidesOpen.action = Selector("revealToggle:")
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.setUpSample()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpSample() {
Alamofire.request(.GET, "http://{url}").responseJSON{ (request, response, data, error) in
var json = JSON(data!)
for var index = 0; index < json["store"].count; ++index{
var marketinfo = json["store"][index]["name"].stringValue
var imageUrl = json["store"][index]["img"].stringValue
let sample_menu = sample(marketInfo: marketinfo, imageName: imageUrl, button: "")
self.apps.append(sample_menu)
}
}
print(self.apps)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return apps.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print(setUpSample())
let cell: TblCell = tableView.dequeueReusableCellWithIdentifier("marketCell") as! TblCell
let sample = apps[indexPath.row]
cell.setCell(sample.marketInfo , imageName: sample.imageName, Coupon: "s/t.jpeg")
return cell
}
}
Within setUpSample function I have got one problem that I have no idea to passing or taking out JSON data. In the function I tried to print result what I can have from it however, the result was empty.