简体   繁体   中英

The application crashes after adding an animation to the tableview (Swift)

I'm trying to adding an animation to insertRows in a tableView when it loading, these are the animation func i created

func insertRowsMode3() {  
   insertRowMode3(ind: 0)   
}

func insertRowMode3(ind:Int) {

   let indPath = IndexPath(row: ind, section: 0)
   rows = ind + 1
   tableView.beginUpdates()
   tableView.insertRows(at: [indPath], with: .right)
   tableView.endUpdates()

   guard ind < rows-1 else { return }
   DispatchQueue.main.asyncAfter(deadline: .now()+0.20) {

       self.insertRowMode3(ind: ind+1)
   }

} 

these are my numberOfRow and numberOfSection

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

    print("numberOfsection Call")
    return 1
}

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

    print("numberOfRows Call")
    if places.count < self.numberPlaces {
       return  places.count
    }
    return self.numberPlaces
}

and this is my viewDidAppear

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

numberPlaces, places and rows are Int variable, anyhow my application is crashing by this error

"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'" .

I have already tried to replace rows with numberPlaces but the application is still crashing, what i have to do to solve this problem?

You insert a new row there, but you did not update the model behind it. There is an incosistence after that - this method will have to update the number of rows too.

There has to be consistence between the model and the UI all the time! So if in time n the numberOfRowsInSection returns 3, and there are already 3 rows in tableView , then if in time n + 1 you call insertRows to add a row, then you have to make sure that numberOfRowsInSection in that time will return 4 (that is 3 + 1, since you added 1 row). Therefore you will have to rework your whole code.

You can try something in the following manner:

// use an additional array for the rows to add them to tableView
var currentPlaces: [Place] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // use this as numberOfRowsInSection
    return currentPlaces.count
}

func insertRowMode3(ind: Int) {

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

    // the model behind the tableView HAS to match the inserts/deletes
    currentPlaces.append(places[ind])
    tableView.insertRows(at: [indPath], with: .right)

    // use proper guard
    guard ind < places.count - 1 else { return }
    DispatchQueue.main.asyncAfter(deadline: .now()+0.20) {
        self.insertRowMode3(ind: ind+1)
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM