Como excluir linha na TTTableViewController (Three20)
Pergunta
O Three20 é grande biblioteca. Está fazendo o trabalho com mesas muito fácil. Mas uma lacuna que eu observei - é exclusão de linhas, com animação. Passei muitas horas tentando fazer isso, e é isso que eu estou fazendo:
// get current index patch
UITableView* table = [super tableView];
NSIndexPath *indexPath = [table indexPathForSelectedRow];
//delete row in data source
TTListDataSource * source = (TTListDataSource *)self.dataSource;
[source.items removeObjectAtIndex:indexPath.row];
// delete row from the table, with animation
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Após esse código é executado o aparece o erro: [NSCFArray objectAtIndex:]: índice (0) além dos limites (0)
Eu estou usando a classe herdada do TTTableViewController. E eu não implementa a DataSourceDelegate ou TableViewDelegate porque eu estou usando o Three20, tanto quanto possível. Então, é assim que eu criar fonte de dados:
for(NSDictionary *album in (NSArray *)albums){
TTTableRightImageItem *item = [TTTableRightImageItem itemWithText:@"name"
imageURL:@"image url"
defaultImage:nil
imageStyle:TTSTYLE(rounded)
URL:@"url of selector where I actually catch the tap on a row"];
[tableItems addObject:item];
}
self.dataSource = [TTListDataSource dataSourceWithItems:tableItems];
Eu li este artigo ( ), mas não ajuda: (
Então, eu sei que isso é muito simples para gurus deste site. Pode me dar exemplo avançado de como fazer isso
Graças
Solução
O primeiro ponto coisa que é que você está excluindo uma linha da tabela quando a tabela não está no modo de edição.
Outras dicas
Eu também tenho "EXC_BAD_ACCESS" em model:didDeleteObject:atIndexPath:
mas eu tenho esse problema resolvido.
Para excluir uma linha em uma TTTableViewController você precisa implementar tableView:commitEditingStyle:
em sua implementação DataSource como esta:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { id object = [[self.items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; [self.model didDeleteObject:object atIndexPath:indexPath]; } }
Você também precisa implementar tableView:willRemoveObject:atIndexPath:
em sua fonte de dados:
- (NSIndexPath*) tableView:(UITableView *)tableView willRemoveObject:(id)object atIndexPath:(NSIndexPath *)indexPath { // delete the object [object deleteFromDB]; // removeItemAtIndexPath returns YES if the section is removed if ([self removeItemAtIndexPath:indexPath andSectionIfEmpty:YES]) { // remove an index path only to that section return [NSIndexPath indexPathWithIndex:indexPath.section]; } else { return indexPath; } }
O truque é mudar o NSIndexPath
você retornar apenas para conter a secção de ele fica vazio. Em seguida, o código no TTTableViewController remove a seção, em vez da célula.
HTH