UivolableView InserTorsaTindexaths Errori.
-
13-11-2019 - |
Domanda
Ho guardato attraverso quasi tutti i risultati di ricerca, ma il mio errore non va via.Ho una vista da tavolo inizializzata con 2 sezioni e 1 riga ciascuna (da un array).Ho un pulsante per la vista della sezione.Sul clic su un pulsante voglio aggiungere una riga alla prima sezione.Ecco il codice:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:@"DetailCell"];
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor clearColor];
return cell;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
btnReleases=[UIButton buttonWithType:UIButtonTypeCustom];
[btnReleases setFrame:CGRectMake(10, 0, 30, 39)];
[btnReleases setImage:[UIImage imageNamed:@"buttonr.png"] forState:UIControlStateNormal];
[btnReleases addTarget:self action:@selector(loadReleases) forControlEvents:UIControlEventTouchUpInside];
return btnReleases;
break;
case 1:
btnLinks=[UIButton buttonWithType:UIButtonTypeCustom];
[btnLinks setFrame:CGRectMake(10, 0, 30, 39)];
[btnLinks setImage:[UIImage imageNamed:@"buttonl.png"] forState:UIControlStateNormal];
[btnLinks addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
return btnLinks;
break;
default:
break;
}
}
-(void)loadReleases
{
[self.myTableView beginUpdates];
[arr addObject:@"WTF"];
NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; NSLog(@"%@",insert0);
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
[self.myTableView endUpdates];
}
.
Ecco l'errore:
.* Insufficienza asserzione in - [ueivolationView _endcellanimationswithContext:], /sourcecache/uikit_sim/uikit-1912.3/uikableview.m:1046
Soluzione
Dal momento che stai usando [arr count]
come il valore di ritorno per tableView:numberOfRowsInSection:
, alla fine del blocco beginUpdates…endUpdates
, la tabella si aspetta che ci siano due righe in ciascuna delle sezioni di tabelle, ma la chiamata a insertRowsAtIndexPaths:
indica solo che una rigaè essere alla sezione 0.
È necessario correggere il tableView:numberOfRowsInSection:
per restituire valori diversi a seconda della sezione:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [arr count];
} else {
return 1;
}
}
.
Nota che ci sono un sacco di cose di pesca in corso con la tua tavola: hai due sezioni, ma stai mostrando la stessa riga esatta 0 in ogni sezione.E il comportamento di inserzione all'interno delle tue sezioni è piuttosto wonky: si inizia a mostrare una sola riga, corrispondente alla custodia 0 nel tuo switch
, quindi indici che stai inserendo una riga alla riga 0, dopo di che mostrerai il caso0 Fiw alla riga 0 e la fila 1 riga alla riga 1. Quindi dovresti davvero inserire una riga alla riga 1 invece di riga 0:
NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
. Altri suggerimenti
prova con il seguente codice
NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
[[self myTableView] beginUpdates];
[[self myTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
[[self myTableView] endUpdates];
.