Norway


When the button is tapped to segue to the tableview, it takes about 5 seconds for it to segue. After it finally segues, when the tableview scrolls, it and sometimes crashes. The tableview is from a json and references images. The images are optimized to low sizes. What is causing this and how can I optimize/fix my code to stop this from happening?

import UIKit

class PDList: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var pdTableView: UITableView!

var pdArt = [Decode]()

override func viewDidLoad() {
    super.viewDidLoad()

    json()

    pdTableView.delegate = self
    pdTableView.dataSource = self

}

func json() {

    let path = Bundle.main.path(forResource: "palmdesert", ofType: "json")
    let url = URL(fileURLWithPath: path!)

    do {
        let  = try (contentsOf: url)
        self.pdArt = try JSONDecoder().decode([Decode].self, from: )
    }
    catch {}

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pdArt.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "pdCell")
    let image = UIImage(named: pdArt[indexPath.row].pic)
    cell.textLabel?.text = pdArt[indexPath.row].art
    cell.detailTextLabel?.text = pdArt[indexPath.row].artist.capitalized
    cell.imageView?.image = image 

    return cell
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "pdDetail", sender: self)
    self.pdTableView.deselectRow(at: indexPath, animated: true)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? PDDetail {
        destination.pdArt = pdArt[(pdTableView.indexPathForSelectedRow?.row)!]
    }
}

@IBAction func done(sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

}

JSON Example:

{
  "art": "Agave",
  "desc": "Agave by Michael Watling serves as the City's entry monument and is inspired by the imagery of the agave, a succulent native to the desert.  The stone forms are representative of Palm Desert's many well-managed resources for survival and growth.",
  "artist": "Michael Watling",
  "lat": 33.7215,
  "long": -116.362,
  "pic": "test.png"
  }

Profile:
Time Profile  - p3grL - ios – swift: uitableview populated from a local json file stutters heavily while scrolling
Time Profile2  - azxcO - ios – swift: uitableview populated from a local json file stutters heavily while scrolling

Let me know if you need any other information. Any help is appreciated.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here