Erros UITableView insertRowsAtIndexPaths
-
13-11-2019 - |
Pergunta
Examinei quase todos os resultados da pesquisa, mas meu erro não desaparece.Eu tenho uma visualização de tabela inicializada com 2 seções e 1 linha cada (de uma matriz).Eu tenho um botão para visualizar o cabeçalho da seção.Ao clicar em um botão, quero adicionar uma linha à primeira seção.Aqui está o código:
- (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];
}
Aqui está o erro:
* Falha de declaração em -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
Solução
Já que você está usando [arr count]
como o valor de retorno para tableView:numberOfRowsInSection:
, no final de beginUpdates…endUpdates
bloquear o tableView espera que haja duas linhas em cada uma das seções do tableView, mas sua chamada para insertRowsAtIndexPaths:
indica apenas que uma linha está indo para a seção 0.
Você precisa consertar seu tableView:numberOfRowsInSection:
para retornar valores diferentes dependendo da seção:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [arr count];
} else {
return 1;
}
}
Observe que há muitas coisas suspeitas acontecendo com o seu tableView:você tem duas seções, mas está mostrando exatamente a mesma linha 0 em cada seção.E o comportamento de inserção nas suas seções é bastante instável:você começa mostrando apenas uma linha, correspondente ao caso 0 no seu switch
, então você indica que está inserindo uma linha na linha 0, após o que mostrará a linha do caso 0 na linha 0 e a linha do caso 1 na linha 1.Então você realmente deveria inserir uma linha na linha 1 em vez da linha 0:
NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
Outras dicas
tente com o código abaixo
NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
[[self myTableView] beginUpdates];
[[self myTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
[[self myTableView] endUpdates];