Il problema reale è il modo in cui funzionano i tableViewControllers dinamici e statici.
Se si crea un UITableViewController dinamico, la maggior parte dei metodi restituisce zero o 0 o un altro valore predefinito. Quindi non succede niente di male se non li sovrascrivi nella sottoclasse UITableViewController.
Se si crea un UITableViewController statico, la maggior parte dei metodi "chiede" allo storyboard cosa restituire. In realtà c'è probabilmente qualcosa come un array privato come backing store che contiene tutti i dati necessari. E se non si sovrascrivono i metodi che interrogano questo backing-store, l'implementazione predefinita chiederà all'array gli oggetti negli indici che non esistono.
Il tuo problema è che dici al TableView che ha 2 sezioni e un paio di righe.Quindi tableView chiede al controllore statico UITableViewController su cose come l'altezza della cella nella seconda riga nella seconda sezione. Ma questo indexPath non esiste nel backing store (perché si inserisce una sola riga nella seconda sezione) che viene utilizzata dalla tabella statica.
Quindi è necessario implementare un paio di metodi DataSource e delegare e restituire i propri valori se il TableView vuole accedere a informazioni che non esistono nell'archivio statico.
Se si guarda lo stack di chiamate dell'eccezione, si dovrebbe essere in grado di vedere questi metodi.
Es .:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ebaaf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010e843bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010eaa301e -[__NSArrayI objectAtIndex:] + 190
3 UIKit 0x000000010f4dc856 -[UITableViewDataSource tableView:heightForRowAtIndexPath:] + 109
4 UIKit 0x000000010f21026b __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 302
5 UIKit 0x000000010f20f8fe -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 4125
6 UIKit 0x000000010f214e45 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 320
7 UIKit 0x000000010f214f3a -[UITableViewRowData heightForTable] + 56
Al indice 3 dello stack di chiamate si può vedere che -[UITableViewDataSource tableView:heightForRowAtIndexPath:]
ha chiamato il codice objectAtIndex:
che ha sollevato l'eccezione. Questo è uno dei metodi che trasmettono le loro chiamate al backend store se non si impedisce loro di farlo. Quindi devi implementare questo metodo e restituire qualcosa di utile. E poi continui fino a quando non ci saranno più eccezioni.
Quanti metodi sono necessari potrebbero dipendere dalla configurazione di TableView. Ho implementato i quattro che di solito causano queste eccezioni, in modo da poter vedere il modello si dovrebbe seguire se si vede più eccezioni che sono causati da metodi/delegato UITableViewDataSource non sovrascritti:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat height = 0;
if (section == 0) {
// static section
height = [super tableView:tableView heightForHeaderInSection:section];
}
return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
CGFloat height = 0;
if (section == 0) {
// static section
height = [super tableView:tableView heightForFooterInSection:section];
}
return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 44;
if (indexPath.section == 0) {
// static section
height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
return height;
}
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger indentationLevel = 0;
if (indexPath.section == 0) {
// static section
indentationLevel = [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
}
return indentationLevel;
}
e qui è un po ' trucco per rendere i contenuti statici ancora più indipendente dal codice:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
// static section
return [super tableView:tableView numberOfRowsInSection:section];
}
return self.objects.count;
}
Se si mescolano le celle dinamiche e statiche, chiamando super
diventa molto utile.
un'occhiata a questo http://stackoverflow.com/a/9428324/285190 – Flexicoder
non vedo come il vostro collegamento dovrebbe essere aiutare a questo caso in quanto i problemi descritti non sono uguali ai miei, inoltre sto usando un UITableViewController e non un UIViewController – CRE8IT
si blocca subito dopo numberOfRowsInSection e nemmeno in CellForRowAtIndexPath – CRE8IT