ios - 单元格或标题 View 高度的 UITableViewAutomaticDimension 导致 UITableView insertRowsAtIndexPaths() 崩溃

标签 ios objective-c iphone swift uitableview

我的 UITableView 有问题。我正在尝试创建一个带有展开/折叠部分的 UITableView。我有固定数量的部分,当用户点击此部分标题 View 时,我想使用 insertRowsAtIndexPaths() 展开部分并使用 deleteRowsAtIndexPaths() 折叠部分。部分的状态存储在 tableSections Array 中。

仅当 Header View 或 Cell 的高度设置为 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) 

最佳答案

我在一年多前将其作为错误(最初是 TSI 请求)提交给 Apple,它被标记为重复,但原始文件仍处于打开状态。看起来我们仍然需要为具有动态部分数的 TableView 计算标题的高度。

关于ios - 单元格或标题 View 高度的 UITableViewAutomaticDimension 导致 UITableView insertRowsAtIndexPaths() 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38477121/

相关文章:

iphone - 我的导航栏没有被隐藏,为什么?

iphone - iOS Game Center 提交时间已过

ios - 崩溃报告工具的实际工作原理

iphone - AVAudioPlayer:MP3 文件无法在设备上播放,但可以在模拟器 (iOS 6) 中播放

ios - 如何以干净的方式更改 CAGradientLayer 属性时防止动画

iphone - 退出后11秒内恢复应用时崩溃(0)

ios - 如何使用swift获取appdelegate中的当前位置

ios - NSArray 的大小

iphone - 如何在 coreplot 中创建静态线

iphone - 显示键盘时以相同的速率移动 UIView