2016-07-20 2 views
1

У меня проблема с моим UITableView. Я пытаюсь создать UITableView с расширяющимися/свертывающимися разделами. У меня есть постоянное количество разделов, и я хочу развернуть раздел с insertRowsAtIndexPaths() и свернуть с deleteRowsAtIndexPaths(), когда пользователь коснется этого раздела. Состояния секций хранятся в tableSectionsArray.UITableViewAutomaticDimension для ячейки или заголовка. Высота высоты вызывает сбой в UITableView insertRowsAtIndexPaths()

Аварийный сигнал возникает только в том случае, если высота заголовка или ячейки установлена ​​на UITableViewAutomaticDimension. Конечно, также установлены estimatedHeightForRowAtIndexPath и estimatedHeightForHeaderInSection.

Вот пример кода:

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

Я подал это как ошибку (первоначально запрос ЦИ) в компании Apple больше, чем год назад, он был отмечен как дубликат, и оригинал все еще открыт. Похоже, нам еще нужно рассчитать высоту заголовков для табличных представлений с динамическим количеством разделов.

Смежные вопросы