简体   繁体   English

如何在TTTableViewController中删除行(Three20)

[英]How to delete row in TTTableViewController (Three20)

The Three20 is great library. Three20是很棒的图书馆。 It's making the work with tables very easy. 这使得使用表非常容易。 But one gap that I noticed - is deletion of rows, with animation. 但是我注意到的一个不足是删除带有动画的行。 I spent many hours trying to do this, and this is what I'm doing: 我花了很多时间试图做到这一点,这就是我正在做的:

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

After this code runs the error appears: [NSCFArray objectAtIndex:]: index (0) beyond bounds (0) 此代码运行后,错误出现:[NSCFArray objectAtIndex:]:索引(0)超出范围(0)

I'm using the class inherited from the TTTableViewController. 我正在使用从TTTableViewController继承的类。 And I doesn't implement the DataSourceDelegate or TableViewDelegate because I'm using the Three20 as much as possible. 而且我没有实现DataSourceDelegate或TableViewDelegate,因为我尽可能多地使用Three20。 So, this is how I create datasource: 因此,这就是我创建数据源的方式:

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

I read this article ( article ) but it doesn't help :( 我读了这篇文章( article ),但没有帮助:(

So, I know that this is very simple for gurus from this site. 因此,我知道对于本网站的专家来说,这非常简单。 Can you just give me advanced sample of how to do this 您能给我高级示例如何做吗

Thanks 谢谢

I also got "EXC_BAD_ACCESS" in model:didDeleteObject:atIndexPath: but I got that problem solved. 我在model:didDeleteObject:atIndexPath:也得到了“ EXC_BAD_ACCESS” model:didDeleteObject:atIndexPath:但是我解决了这个问题。

To delete a row in a TTTableViewController you need to implement tableView:commitEditingStyle: in your DataSource implementation like this: 要删除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];
    }
}

You also need to implement tableView:willRemoveObject:atIndexPath: in your DataSource: 您还需要在数据源中实现tableView:willRemoveObject:atIndexPath: ::

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

The trick is to change the NSIndexPath you return to only contain the section of it gets empty. 诀窍是将您返回的NSIndexPath更改为仅包含其变为空的部分。 Then the code in TTTableViewController removes the section instead of the cell. 然后,TTTableViewController中的代码将删除该部分而不是单元格。

HTH HTH

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Three20 TTTableViewController + NSFetchedResultsController - Three20 TTTableViewController + NSFetchedResultsController 如何在加载时隐藏 Three20 TTTableViewController tableView - How to hide Three20 TTTableViewController tableView while Loading Three20 TTTableViewController具有多个模型吗? 这可能吗? - Three20 TTTableViewController with multiple models? Is this possible? three20 - TTTableViewController内存警告给出了空白屏幕,如何修复? - three20 - TTTableViewController Memory warning gives blank screen, how to fix? 如何在three20的TTTableViewController中获取用户定义的背景颜色 - How to get user defined background color in three20's TTTableViewController 数据源项和TTTableViewController之间的Three20通信 - Three20 comunication between datasource items and TTTableViewController 将TTTableViewController(从three20)添加到另一个UIViewController - Adding a TTTableViewController (from three20) to another UIViewController three20 TTTableViewController拖动以刷新:无连接时没有错误屏幕 - three20 TTTableViewController drag to refresh: no error screen on no connection 奇怪的three20的TTTableViewController框架属性 - Strange three20's TTTableViewController frame properties Three20 - 将 TTTableViewController 设置为 TableBannerView 时获得空的 TableView - Three20 - Getting empty TableView when setting a TTTableViewController as TableBannerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM