2

Three20 是很棒的图书馆。它使使用表格的工作变得非常容易。但我注意到的一个差距 - 是删除行,带有动画。我花了很多时间试图做到这一点,这就是我正在做的事情:

// 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];

此代码运行后出现错误:[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)

我正在使用从 TTTableViewController 继承的类。而且我没有实现 DataSourceDelegate 或 TableViewDelegate,因为我尽可能多地使用 Three20。所以,这就是我创建数据源的方式:

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];

我读了这篇文章(文章),但没有帮助:(

所以,我知道这对于这个网站的大师来说非常简单。你能不能给我一些如何做到这一点的高级样本

谢谢

4

2 回答 2

4

我也得到了“EXC_BAD_ACCESS”,model:didDeleteObject:atIndexPath:但我解决了这个问题。

要删除 TTTableViewController 中的一行,您需要tableView:commitEditingStyle:在 DataSource 实现中像这样实现:

- (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];
    }
}

您还需要tableView:willRemoveObject:atIndexPath:在 DataSource 中实现:

- (NSIndexPath*) tableView:(UITableView *)tableView willRemoveObject:(id)object atIndexPath:(NSIndexPath *)indexPath {
    // 删除对象
    [对象 deleteFromDB];

    // 如果节被删除,removeItemAtIndexPath 返回 YES
    if ([self removeItemAtIndexPath:indexPath andSectionIfEmpty:YES]) {
        // 仅删除该部分的索引路径
        返回 [NSIndexPath indexPathWithIndex:indexPath.section];
    } 别的 {
        返回索引路径;
    }
}

诀窍是将NSIndexPath返回的内容更改为仅包含它的部分为空。然后 TTTableViewController 中的代码删除了部分而不是单元格。

高温高压

于 2010-03-08T13:33:41.823 回答
1

我发现的第一件事是,当表格未处于编辑模式时,您正在从表格中删除一行。

于 2009-12-06T19:06:32.513 回答