【问题标题】:How to delete row in TTTableViewController (Three20)如何在 Table View Controller 中删除行(Three20)
【发布时间】:2009-12-06 13:53:39
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: iphone three20


    【解决方案1】:

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

    要删除 TTTableViewController 中的一行,您需要在 DataSource 实现中实现 tableView:commitEditingStyle:,如下所示:

    - (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:

    - (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 中的代码删除了部分而不是单元格。

    HTH

    【讨论】:

    • 我想知道你为什么在数据源的tableView:willRemoveObject: atIndexPath: 方法中调用[self.model didDeleteObject:object atIndexPath:indexPath];。你不应该调用[self.model removeSomeObject:... atIndexPath:indexPath] 并让这个对象调用[self didDeleteObject:object atIndexPath:indexPath] 吗?
    【解决方案2】:

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

    【讨论】:

    • 好的。这是很重要的评论。我在“NSIndexPath *indexPath = [table indexPathForSelectedRow];”行之后添加了“table.editing = YES;”行。结果是一样的:(
    • TTSectionedDataSource 类不足以在可编辑表中使用,因为它没有实现 tableView:commitEditingStyle:forRowAtIndexPath。子类 TTSectionedDataSource 并实现ableView:commitEditingStyle:forRowAtIndexPath。在此方法中删除项目,然后调用 didDeleteObject。此方法将通知您的控制器并执行动画。
    • 实际上我只需要以编程方式使用动画删除该行。喜欢这里:stackoverflow.com/questions/915095/…,但使用 Three20。我注意到在 TTSectionedDataSource 中有“- (void)removeItemAtIndexPath:(NSIndexPath*)indexPath”方法。但它抛出一个错误!也许这是 Three20 中的一个错误??
    • 如果我没记错的话,TTSectionedDataSource 中的成员项是一个或多个数组 - 每个部分一个。在您的代码中,您将删除整个数组,这将删除该部分中的所有项目。所以你应该这样做: NSArray* section = [source.items objectAtIndex:indexPath.section]; [section removeObjectAtIndex:indexPath.row];
    • 哼,是真的。我已经修改了上面的源代码。但现在我看到了奇怪的行为。当我运行上面的代码时,我看到行删除,并且在动画完成后 - 应用程序终止并出现错误“程序接收信号:“EXC_BAD_ACCESS”。”
    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多