klevison / AccordionTableViewController

Swift version of https://github.com/klevison/KMAccordionTableViewController
MIT License
46 stars 2 forks source link

tableview endupdate() crash #7

Open chetandobariya opened 7 years ago

chetandobariya commented 7 years ago

Hello there,

I got application crash every time when I try to close the previous section and open a new section.

Crash happened at line tableview.endupdate() in below code ,

 fileprivate func sectionHeaderViewOpenedAtIndex(_ index: Int) {

        let section = sections[index]
        section.open = true

        var indexPathsToInsert = [IndexPath]()
        var indexPathsToDelete = [IndexPath]()
        indexPathsToInsert.append(IndexPath(row: 0, section: index))

        if openedSectionIndex != NSNotFound {
            let previousOpenSection = sections[openedSectionIndex]
            let praviousSectionHeaderView = tableView.headerView(forSection: openedSectionIndex) as! SectionHeaderView
            praviousSectionHeaderView.disclosureButton.isSelected = false
            previousOpenSection.open = false
            indexPathsToDelete.append(IndexPath(row: 0, section: openedSectionIndex))
            delegate?.accordionTableViewControllerSectionDidClose(previousOpenSection)
        }

        tableView.beginUpdates()
        tableView.insertRows(at: indexPathsToInsert, with: openAnimation)
        tableView.deleteRows(at: indexPathsToDelete, with: closeAnimation)
        tableView.endUpdates()

        let sectionRect = tableView.rect(forSection: index)
        tableView.scrollRectToVisible(sectionRect, animated: true)

        openedSectionIndex = index

        delegate?.accordionTableViewControllerSectionDidOpen(section)
    }

error : Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 0 from section 1 which only contains 0 rows before the update'

Please let me know if there is any update in the library or if I miss something in above logic. Any help will be appreciated :)

klevison commented 7 years ago

Can you check indexPathsToInsert and indexPathsToDelete values? Can you show me your code?

chetandobariya commented 7 years ago

@klevison indexPathsToInsert.count = 1 and indexPathsToDelete.count = 1

Debug description :

indexPathsToInsert = [[4, 0]] indexPathsToDelete = [[1, 0]]

klevison commented 7 years ago

Can you show your viewcontroller source code?

chetandobariya commented 7 years ago

my view controller is very complex and has 4 level hierarchy.. but I hope this information will help you to figure out.

I am calling below method to update UI after calling delegate: fileprivate func sectionHeaderViewOpenedAtIndex(_ index: Int) {}

 func reloadOpenedSection(_ animation: Bool) {
        if animation == false {
            openAnimation = UITableViewRowAnimation.none
        }

        if let openedSection = getOpenedSection() {
            let indexPath = IndexPath(row: 0, section: openedSection.sectionIndex!)
            let cellToReloadArray = [indexPath]
            tableView.reloadRows(at: cellToReloadArray, with: openAnimation)
        }
    }
chetandobariya commented 7 years ago

you can have a look at attached Screen.

The application will crash on below steps:

  1. expand Style Edit -1st level (section no: 6)
  2. expand Weddings - 2nd level
  3. now click on Vacation Shop - 2nd level

img_1320

Boommmmm!!!!- application crash with above-mentioned error.

klevison commented 7 years ago

1) If you comment all sectionHeaderViewOpenedAtIndex code (reloadOpenedSection) what happens?

chetandobariya commented 7 years ago

Then the table has not expanded any category. it will show only 1st level.

klevison commented 7 years ago

When I asked about indexPathsToInsert and indexPathsToDelete you have shown me: indexPathsToInsert = [[4, 0]] indexPathsToDelete = [[1, 0]]

Is this values are correct? It should:

1) close section row 1 into section 0 2) Open row 4 into section 0

chetandobariya commented 7 years ago

@klevison I think 0 is the row and 1 & 4 is sections. as per your written code. Please have a look at this line

  indexPathsToInsert.append(IndexPath(row: 0, section: index))
  indexPathsToDelete.append(IndexPath(row: 0, section: openedSectionIndex))

and these values are correct because I am closing "wedding ", section 1 and try to open "vacation shop", section 4 as per my attached snapshot. (I am talking about sections on the second level)

in the crash, I am not performing any action on the section 0. Please let me know if I am wrong.

chetandobariya commented 7 years ago

@klevison dear owner... app crash a lot due to AccordionTableViewController library.

please have a look at Crashlytics report.

screen shot 2017-04-28 at 09 52 22

Here you can find more info about crash (source: Crashlytics)

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x181806fd8 __exceptionPreprocess
1  libobjc.A.dylib                0x180268538 objc_exception_throw
2  CoreFoundation                 0x181806eac +[NSException raise:format:]
3  Foundation                     0x18229e710 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  UIKit                          0x187b12dec -[UITableView _endCellAnimationsWithContext:]
5  XXXXX                          0x100121ca0 AccordionTableViewController.sectionHeaderViewOpenedAtIndex(Int) -> () (AccordionTableViewController.swift)
6  XXXXX                          0x100124710 specialized AccordionTableViewController.sectionHeaderView(SectionHeaderView, selectedAtIndex : Int) -> () (AccordionTableViewController.swift)
7  XXXXX                          0x100122a60 @objc AccordionTableViewController.sectionHeaderView(SectionHeaderView, selectedAtIndex : Int) -> () (AccordionTableViewController.swift)
8  XXXXX                          0x100122abc protocol witness for SectionHeaderViewDelegate.sectionHeaderView(SectionHeaderView, selectedAtIndex : Int) -> () in conformance AccordionTableViewController (AccordionTableViewController.swift)
9  XXXXX                          0x1000ee35c specialized SectionHeaderView.toggleOpen(AnyObject) -> () (SectionHeaderView.swift:63)
10 XXXXX                          0x1000ed730 @objc SectionHeaderView.toggleOpen(AnyObject) -> () (SectionHeaderView.swift)
11 UIKit                          0x187f07c54 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:]
12 UIKit                          0x187f0b488 _UIGestureRecognizerSendTargetActions
13 UIKit                          0x187ac5540 _UIGestureRecognizerSendActions
14 UIKit                          0x18796745c -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:]
15 UIKit                          0x187efb72c _UIGestureEnvironmentUpdate
16 UIKit                          0x187efb2fc -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:]
17 UIKit                          0x187efa5a8 -[UIGestureEnvironment _updateGesturesForEvent:window:]
18 UIKit                          0x1879657e8 -[UIWindow sendEvent:]
19 UIKit                          0x187936418 -[UIApplication sendEvent:]
20 UIKit                          0x18812ff64 __dispatchPreprocessedEventFromEventQueue
21 UIKit                          0x18812a6c0 __handleEventQueue
22 UIKit                          0x18812aaec __handleHIDEventFetcherDrain
23 CoreFoundation                 0x1817b5424 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
24 CoreFoundation                 0x1817b4d94 __CFRunLoopDoSources0
25 CoreFoundation                 0x1817b29a0 __CFRunLoopRun
26 CoreFoundation                 0x1816e2d94 CFRunLoopRunSpecific
27 GraphicsServices               0x18314c074 GSEventRunModal
28 UIKit                          0x18799b130 UIApplicationMain
29 XXXXX                          0x10009d65c main (AppDelegate.swift:17)
30 libdyld.dylib                  0x1806f159c start
AnalyticsTeamIndia commented 5 years ago

Hi Chetandobariya Did you get a chance to fix this crash? Please let me know if you have figured out a way to address this issue.