[英]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.