Ich habe ein Problem mit meinem UITableView
. Ich versuche, eine UITableView
mit erweitern/reduzieren Abschnitte zu erstellen. Ich habe eine konstante Anzahl von Abschnitten und ich möchte Abschnitt mit insertRowsAtIndexPaths()
erweitern und mit deleteRowsAtIndexPaths()
zusammenklappen, wenn Benutzer auf diesen Abschnitt Kopfzeile tippen. Zustände von Abschnitten werden in einem tableSections
Array
gespeichert.UITableViewAutomaticDimension für Zelle oder Header Ansicht Höhe verursacht Absturz auf UITableView insertRowsAtIndexPaths()
Der Absturz tritt nur auf, wenn die Höhe der Kopfzeile oder Zelle auf UITableViewAutomaticDimension
eingestellt ist. Natürlich sind auch estimatedHeightForRowAtIndexPath
und estimatedHeightForHeaderInSection
eingestellt.
Hier ist ein Beispielcode:
enum SectionState: Int {
case closed, opened
mutating func changeState() {
let currentState = self
switch currentState {
case .closed:
self = .opened
break
case .opened:
self = .closed
break
}
}
}
private class TableSection: NSObject {
var sectionState: SectionState = .closed
}
var sectionData = ["a", "b", "c", "d"]
let numberOfSections = 10
var tableSections: Array<TableSection> = []
// ---------------------
override func viewDidLoad() {
super.viewDidLoad()
setupTableSections()
}
func setupTableSections() {
tableSections.removeAll()
for section in 0..<numberOfSections {
let section = TableSection()
tableSections.append(section)
}
}
func openCloseSectionAction(sender: UIButton) {
let btnOpenCloseSection = sender
guard let headerView = btnOpenCloseSection.superview else { return }
let selectedSection = sectionNumberForView(headerView, inTableView: tableView)
tableSections[selectedSection].sectionState.changeState()
let indexPaths: [NSIndexPath] = {
var indexPaths:[NSIndexPath] = []
for row in 0..<sectionData.count {
indexPaths.append(NSIndexPath(forRow: row, inSection: selectedSection))
}
return indexPaths
}()
tableView.beginUpdates()
switch tableSections[selectedSection].sectionState {
case .closed:
tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Top)
break
case .opened:
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Bottom)
break
}
tableView.endUpdates()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return numberOfSections
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch tableSections[section].sectionState {
case .closed:
return 0
case .opened:
return sectionData.count
}
}
Und es gibt Absturzprotokolle:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
(lldb) bt
* thread #1: tid = 0x58a0d, 0x000000018126011c libsystem_kernel.dylib`__pthread_kill + 8, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x000000018126011c libsystem_kernel.dylib`__pthread_kill + 8
frame #1: 0x000000018132cef8 libsystem_pthread.dylib`pthread_kill + 112
frame #2: 0x00000001811d1dac libsystem_c.dylib`abort + 140
frame #3: 0x0000000180d053f4 libc++abi.dylib`abort_message + 132
frame #4: 0x0000000180d21e98 libc++abi.dylib`default_terminate_handler() + 304
frame #5: 0x0000000180d2c248 libobjc.A.dylib`_objc_terminate() + 124
frame #6: 0x0000000180d2c248 libobjc.A.dylib`_objc_terminate() + 124
frame #7: 0x0000000180d1ef44 libc++abi.dylib`std::__terminate(void (*)()) + 16
frame #8: 0x0000000180d1eb10 libc++abi.dylib`__cxa_rethrow + 144
frame #9: 0x0000000180d2c120 libobjc.A.dylib`objc_exception_rethrow + 44
frame #10: 0x00000001815a4cf8 CoreFoundation`CFRunLoopRunSpecific + 552
frame #11: 0x0000000182e8c088 GraphicsServices`GSEventRunModal + 180
frame #12: 0x000000018688e088 UIKit`UIApplicationMain + 204
* frame #13: 0x00000001000bc9a4 MyApp`main + 144 at AppDelegate.swift:17
frame #14: 0x00000001811428b8 libdyld.dylib`start + 4
(lldb)