도래울

swift uitableview cell insert delete crash 본문

개발/iOS

swift uitableview cell insert delete crash

도래울 2016. 6. 7. 17:59

            self.tableView.beginUpdates()

            self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Right)

            self.tableView.endUpdates()




함수를 사용하여 증권 시세의 데이터를 삽입 시키는 작업을 하는데


문제가 생겼다. 


*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1145.66/UITableView.m:827

2010-07-17 22:06:13.601 iPadUIBase[21630:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (13) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'


의 에러가 생겨서 무슨 이유인지 계속 몰랐기 때문이다.


하지만 알고보니 TableView에서 마지막으로 가지고 있는 데이터 개수와 현재 삽입하려는


개수의 -N이 맞아 떨어져야 정상적으로 삽입이 되는 것이였다.


처음 테이블의 데이터가 0개인 상태에서 테이블 데이터13개를 한번에 넣고 

insertRowsAtIndexPaths를 날려서 13개가 그려져 있지 않은 상태에서 -1을 

하려고 하니 생기는 에러였다. 


해결 방법은 endUpdates 전에 데이터 리로드하여


인식시키고 추가하려는 개수를 동기화 하면 된다.


Comments