2015-02-19 4 views
0

Я получаю следующую ошибку, пытаясь спроектировать представление таблицы предпочтений.Uncaught Exception in heightForHeaderInSection

2015-02-19 21:19:49.204 MyApp[8337:285561] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000110859b95 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x00000001123aebb7 objc_exception_throw + 45 
    2 CoreFoundation      0x00000001107501fe -[__NSArrayI objectAtIndex:] + 190 
    3 UIKit        0x000000011161df2a -[UITableViewDataSource tableView:heightForHeaderInSection:] + 39 
    4 UIKit        0x00000001111f0e58 -[UITableView _delegateWantsHeaderForSection:] + 261 
    5 UIKit        0x000000011137b183 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162 
    6 UIKit        0x0000000111381645 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 320 
    7 UIKit        0x000000011138173a -[UITableViewRowData heightForTable] + 56 
    8 UIKit        0x00000001111cd870 -[UITableView _updateContentSize] + 381 
    9 UIKit        0x00000001111ec5a3 -[UITableView setContentInset:] + 336 
    10 UIKit        0x000000011121e6ef -[UIViewController _setNavigationControllerContentInsetAdjustment:] + 501 
    11 UIKit        0x000000011eb19b89 -[UIViewControllerAccessibility _setNavigationControllerContentInsetAdjustment:] + 80 
    12 UIKit        0x00000001112541a6 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 432 
    13 UIKit        0x0000000111254323 -[UINavigationController _layoutViewController:] + 116 
    14 UIKit        0x0000000111254825 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 216 
    15 UIKit        0x0000000111249957 -[UINavigationController _startCustomTransition:] + 1657 
    16 UIKit        0x000000011125567f -[UINavigationController _startDeferredTransitionIfNeeded:] + 386 
    17 UIKit        0x00000001112561ce -[UINavigationController __viewWillLayoutSubviews] + 43 
    18 UIKit        0x000000011139eded -[UILayoutContainerView layoutSubviews] + 202 
    19 UIKit        0x000000011eb3351e -[UILayoutContainerViewAccessibility layoutSubviews] + 43 
    20 UIKit        0x0000000111176040 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521 
    21 QuartzCore       0x0000000114c2c38a -[CALayer layoutSublayers] + 146 
    22 QuartzCore       0x0000000114c20c46 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 
    23 QuartzCore       0x0000000114c20ab6 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    24 QuartzCore       0x0000000114b8cf1a _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 
    25 QuartzCore       0x0000000114b8e0a5 _ZN2CA11Transaction6commitEv + 447 
    26 QuartzCore       0x0000000114b8e73f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 
    27 CoreFoundation      0x000000011078cd27 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    28 CoreFoundation      0x000000011078cc80 __CFRunLoopDoObservers + 368 
    29 CoreFoundation      0x0000000110782ab3 __CFRunLoopRun + 1123 
    30 CoreFoundation      0x00000001107823e6 CFRunLoopRunSpecific + 470 
    31 GraphicsServices     0x0000000114512a3e GSEventRunModal + 161 
    32 UIKit        0x00000001110f5b60 UIApplicationMain + 1282 
    33 MyApp     0x000000010fe687f7 main + 135 
    34 libdyld.dylib      0x0000000112b52145 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

я полагаю, что что-то в Tableview: heightForHeaderInSection испортится, но я не могу угадать, что происходит там.

Вот другие соответствующие классы: PreferencesTableViewController

import UIKit 

class PreferencesTableViewController: UITableViewController { 

    let factory = PreferencesCellFactory() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     factory.setTitleForSection(0, title: "Off") 
     factory.createSwitcherCell("Fallback-Modus", subtitle: "No-Subtitle") 
     factory.createEditorCell("Shortname", subtitle: "Shortname") 


     factory.setTitleForSection(1, title: "New") 
     //factory.createEditorCell("New Testcell", subtitle: "Hallo", section: 1) 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return factory.numberOfSections() 

    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete method implementation. 
     // Return the number of rows in the section. 
     return factory.numberOfRowsInSection(section) 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     return factory.getCellForSectionWithIndex(SectionNr: indexPath.section, index: indexPath.row) // returns correct cell 
    } 

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return factory.getTitleForSection(SectioNr: section) // returns correct header 
    } 


} 

PreferencesCellFactory

import Foundation 

class PreferencesCellFactory { 
    var sections : [[UITableViewPreferencesCell]] 
    var sectionTitles : [String] 

    init() { 
     sections  = [[UITableViewPreferencesCell]]() 
     sectionTitles = [String]() 
    } 

    func numberOfSections() -> Int { 
     return sections.count 
    } 


    func numberOfRowsInSection(section: Int) -> Int{ 
     return sections[section].count 
    } 


    func setTitleForSection(index: Int, title: String!){ 
     if sectionTitles.count == index { 
      sectionTitles.append(title) 
     } 
     else { 
     sectionTitles[index] = title 
     } 
    } 



    func createEditorCell(title: String, subtitle: String){ 
     createEditorCell(title, subtitle: subtitle, section: 0) 
    } 


    func createEditorCell(title: String, subtitle: String, section: Int){ 


     if sections.count == section { 
      sections.append([UITableViewPreferencesCell]()) 
     } 

     let cell = UITableViewEditorCell() 
     cell.textLabel?.text   = title 
     cell.detailTextLabel?.text = subtitle 
     sections[section].append(cell) 
     println("Test") 
    } 


    func createSwitcherCell(title: String, subtitle: String){ 
     createSwitcherCell(title, subtitle: subtitle, section: 0) 
    } 


    func createSwitcherCell(title: String, subtitle: String, section: Int){ 
     // Wenn noch keine Sections angelegt sind, muss erst eine initialisiert werden 
     if sections.count == 0 { 
      sections.append([UITableViewPreferencesCell]()) 
     } 

     let cell = UITableViewSwitcherCell() 
     cell.textLabel?.text   = title 
     cell.detailTextLabel?.text = subtitle 
     sections[section].append(cell) 
    } 



    func getCellAtIndex(index: Int) -> UITableViewPreferencesCell{ 
     if sections.count == 0 { 
      NSLog("Sections Array ist leer") 
     } 
     return getCellForSectionWithIndex(SectionNr: 0, index: index) 
    } 



    func getCellForSectionWithIndex(SectionNr section: Int, index: Int) -> UITableViewPreferencesCell{ 
     if sections.count == 0 { 
      NSLog("Sections Array ist leer") 
     } 
     return sections[section][index] 
    } 




    func getCells() -> [UITableViewPreferencesCell]{ 
     if sections.count == 0 { 
      NSLog("Sections Array ist leer") 
     } 
     return getCellsForSection(SectionNr: 0) 
    } 



    func getCellsForSection(SectionNr section: Int) -> [UITableViewPreferencesCell]{ 
     if sections.count == 0 { 
      NSLog("Sections Array ist leer") 
     } 
     return sections[section] 
    } 



    func getTitleForSection(SectioNr section: Int) -> String { 
     if sectionTitles.count == 0 { 
      NSLog("SectionTitles Array ist leer") 
      return "Not titles found" 
     } 
     return sectionTitles[section] 
    } 
} 

Я надеюсь, что кто-то может помочь. При необходимости я могу предоставить более подробную информацию.

Заранее спасибо

PS: Если numberOfSectionsInTableView возвращает 1. не возникает ошибка

#

первый шаг к решению:

я добавил все эти методы, и теперь он, кажется, работает как и предполагалось, но ПОЧЕМУ я должен их реализовать?

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

    override func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

    override func tableView(tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return 50.0 
    } 

    override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int { 
     return 0 
    } 
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
     return UIView() 
    } 
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     return UIView() 
    } 

ответ

1

Проблема заключалась в использовании статических ячеек для таблицы. Это означает, что все упомянутые методы должны быть реализованы. Переход к динамическим ячейкам устранял проблему без проблем.

Если вы используете статические клетки, вы действительно должны реализовать все свойства просмотра, как в headerView и т.д.

1

Ваша задача в этой части:

func getTitleForSection(SectioNr section: Int) -> String { 
     if sectionTitles.count == 0 { 
      NSLog("SectionTitles Array ist leer") 
      return "Not titles found" 
     } 
     return sectionTitles[section] 
    } 

, как вы могли видеть в журнале ошибок, он появится в [__NSArrayI objectAtIndex:] Так что, если вы sectionTitles массив, позволяет сказать, один элемент, и вы спрашиваете 1- st, 2-й или более высокий индекс, тогда ваше приложение будет генерировать исключение (и раздавить). Вы должны проверить количество объектов в этом массиве, прежде чем делать sectionTitles[section]


UPD:

У меня есть вопрос о вашем коде:

Ваш sections в PreferencesCellFactory имеет тип UITableViewPreferencesCell (конечно, это пользовательский класс) , но вы делаете sections[section].append(cell) нижний индекс. Да, вы его реализуете и как это выглядит?

Потому что без него я не могу воспроизвести код, и если я его комментирую, то приложение не раздавит.

+0

но у меня есть, как много названий Аси действительно имеют разделы. почему тогда проблема? – gutenmorgenuhu

+0

Вы на 100 процентов уверены? Лучше поместить что-то вроде метода println() внутри метода getTitleForSection –

+0

Отпечатайте раздел и раздел Title.sount. Не могли бы вы отправить здесь результаты отладки до раздавливания? –

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