2016-07-20 65 views
1

我有我的UITableView問題。我正在嘗試創建一個UITableView的擴展/摺疊節。我有段固定數量的,我想用insertRowsAtIndexPaths()擴大部分與deleteRowsAtIndexPaths()崩潰時,在此節頭查看用戶點擊。各部分的狀態存儲在tableSectionsArray中。UITableViewAutomaticDimension的細胞或標題查看高度會導致崩潰的UITableView的insertRowsAtIndexPaths()

僅當部首查看或細胞的高度被設定爲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) 

回答

0

我提起它作爲一個bug(原本是TSI請求)蘋果一年多前,它被標記爲重複,並且原始文件仍然打開。看起來我們仍然需要爲具有動態數量的部分的表格視圖計算標題的高度。

相關問題