App crush: Terminating app due to uncaught excepti

2019-09-21 07:11发布

问题:

import UIKit
import MapKit
import CoreLocation

class CourseClass2: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITableViewDelegate, UITableViewDataSource {

  @IBOutlet weak
  var map: MKMapView!

    @IBOutlet weak
  var mapCourse: UIImageView!

    @IBOutlet weak
  var tableView: UITableView!

    struct User {

      var name: String
      var images: UIImage
      var coordinate: (Double, Double)
      var type: String
      var address: String
    }

  var rows = 0

  override func viewDidLoad() {
    super.viewDidLoad()

  }

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    insertRowsMode3()
  }

  func insertRowsMode2() {

    for i in 0.. < users.count {
      insertRowMode2(ind: i, usr: users[i])
    }

  }

  func insertRowMode2(ind: Int, usr: User) {

    let indPath = IndexPath(row: ind, section: 0)

    rows = ind + 1
    tableView.insertRows(at: [indPath], with: .right)
  }


  func insertRowsMode3() {

    rows = 0

    insertRowMode3(ind: 0)
  }

  func insertRowMode3(ind: Int) {
    let indPath = IndexPath(row: ind, section: 0)
    rows = ind + 1
    tableView.insertRows(at: [indPath], with: .right)

    guard ind < users.count - 1
    else {
      return
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {

      self.insertRowMode3(ind: ind + 1)
    }
  }

  func numberOfSections( in tableView: UITableView) - > Int {

    return 1
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - > Int {

    return rows
  }

  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
      for: indexPath) as!MyTableViewCell

    let user = users[indexPath.row]

    cell.MyImage.image = user.images
    cell.MyLabel.text = user.name
    cell.MyTypeLabel.text = user.type

    return (cell)
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: "goToLast", sender: users[indexPath.row])

  }

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

  override func prepare(
    for segue: UIStoryboardSegue, sender: Any ? ) {
    if segue.identifier == "goToLast" {

      guard
      let vc = segue.destination as ? FinalClass
      else {
        return
      }

      let guest = segue.destination as!FinalClass

      if let user = sender as ? User {

        guest.local = user.name

        guest.localImage = user.images

        guest.localType = user.type

        guest.localAddress = user.address
      }
    }
  }

  @IBAction func IndTapped(_ sender: Any) {
    self.performSegue(withIdentifier: "goBack", sender: self)

  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }


}

Hello guys, this is the code of the ViewController where i get the error, i added a tableView with a particular animation but when i change the view and than come back with the dismiss(animated: true, completion: nil)the application crash because there is some inconsistency with rows data. As you can see, when i'm first visiting this CourseClass2 controller i'm setting up row by calling insertRowsMode3 in viewDidAppear.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    insertRowsMode3()
}

The problem is when i come back the row data is not same as i have initialised it with zero and viewDidAppear doesn't get called. I know the error but i don't how i can change my code to make it work. I really need an help.

The app crush exactly in this func

func insertRowMode3(ind:Int) {
        let indPath = IndexPath(row: ind, section: 0)
        rows = ind + 1
         tableView.insertRows(at: [indPath], with: .right)

        guard ind < users.count-1 else { return }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.15) {

            self.insertRowMode3(ind: ind+1)
        }
    }

here tableView.insertRows(at: [indPath], with: .right)

回答1:

You mentioned invalid no of rows in section 0 : In insertRowMode3 , you are inserting rows but not updating the table view. So your table view getting the same count of no of rows in table view even afer updating, thats where inconsistency is.

Use tableview.beginUpdates() , then insert rows , increment ur rows value , then tableview.endUpdates()

Let me know if it doesn't help you out.



标签: ios swift xcode