单元格或标题视图高度的UITableViewAutomaticDimension导致UITableView insertRowsAtIndexPaths()崩溃

时间:2016-07-20 09:15:17

标签: ios objective-c iphone swift uitableview

我的UITableView有问题。我正在尝试使用展开/折叠部分创建UITableView。我有一定数量的部分,当我们点击此部分标题视图时,我希望使用insertRowsAtIndexPaths()扩展部分并使用deleteRowsAtIndexPaths()折叠。部分的状态存储在tableSections Array

仅当标题视图或单元格的高度设置为UITableViewAutomaticDimension时才会发生崩溃。当然,estimatedHeightForRowAtIndexPathestimatedHeightForHeaderInSection也已设置。

以下是示例代码:

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
    }
}

还有崩溃日志:

*** 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) 

1 个答案:

答案 0 :(得分:0)

我在一年多前将它作为一个错误(最初是TSI请求)提交给Apple,它被标记为重复版,原版仍然是开放的。看起来我们仍然需要计算具有动态数量的部分的表视图的标题高度。