gpt4 book ai didi

iphone - 如何删除 TableView Controller 中的行(Three20)

转载 作者:行者123 更新时间:2023-12-03 20:37:22 25 4
gpt4 key购买 nike

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)超出边界(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 ),但没有帮助:(

所以,我知道这对于这个网站的专家来说非常简单。您能否给我提供如何执行此操作的高级示例

谢谢

最佳答案

我还在 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 {    // 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;    }}

诀窍是更改您返回的NSIndexPath,使其仅包含其变空的部分。然后 TTTableViewController 中的代码将删除该部分而不是单元格。

HTH

关于iphone - 如何删除 TableView Controller 中的行(Three20),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1855447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com