2

我知道有很多像这样的问题,但我已经关注了其中的几个,但没有一个能引导我找到答案。

可能这只是我想念的愚蠢而明显的东西,但我无法理解...

所以我正在做的是,我有一个普通的 UITableViewController。在表格中,我想显示用户通过我的应用下载的所有文件,并让他也有机会删除这些文件。一切正常 - 我可以列出文件,单击时会打开另一个视图以显示文件。

但我永远无法让该行删除某些内容。我刷了所有可能的方法,但我什至没有得到删除按钮!我已经实现了 canEditRowAtIndexPath 函数以使其返回 YES (虽然它不应该是必需的,但我已经制作了大量的应用程序,我删除了行并且从不需要该函数):

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

我的实际删除方法是这样实现的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSError *error = [[NSError alloc] init];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *fileName = [NSString stringWithFormat:@"%@/%@", directory, [files objectAtIndex:indexPath.row]];
        BOOL success = false;

        if ([fileManager isDeletableFileAtPath:fileName]) {
            success = [fileManager removeItemAtPath:fileName error:&error];
        }

        if (success) {
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [files removeObjectAtIndex:indexPath.row];
        } else {
            [Functions msg:NSLocalizedString(@"fileCantBeDeleted", @"") title:NSLocalizedString(@"fileError", @"") delegate:nil];
            if (error) {
                NSLog(@"Error: %@", [error localizedDescription]);
            }
        }
    }
}

为了完整起见,我也可以给你 cellForRowAtIndexpath 的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [files objectAtIndex:indexPath.row];
    return cell;
}

NSArray“文件”之前已经被填充,在 viewDidLoad 方法中是这样的:

directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:nil];

目录和文件都是我班级的属性。如前所述,整个类只是实现了一个 UITableViewController。我真的不明白为什么它不允许我删除一行,但可能这真的很愚蠢......我只是看不到它。

编辑

发现了错误。

这个特殊的 TableView 本身并没有实现 PKRevealController 库的任何方法,但它显然被设置为 frontViewController - 所以那里碰巧有一个我不知道的手势。该控制器也不打算在 PKRevealController 中使用...我只是以错误的方式添加它,所以我也没有意识到这一点!这也是为什么我忘记将它包含在我的问题中的原因。

我在这里找到了解决方案:https ://github.com/pkluz/PKRevealController/issues/123 (如果其他人可能会遇到这个问题)。

无论如何,谢谢你帮助我!

编辑

我刚刚了解到的另一种可能的解决方案是,如果您想使用 tableView 并且在使用 PKRevealController 时仍然继续删除内容(不会开始对 UIGestureRecognizers 感到陌生):您只需添加一个编辑按钮即可触发视图进入编辑模式。

self.navigationItem.rightBarButtonItem = self.editButtonItem;

这对我来说非常有效。

4

2 回答 2

3

使您的表格视图可编辑

您需要创建表格视图的对象....

在 ViewController 的 .h 文件中。

UITableView *table;

在 ViewController 的 .m 文件中,您还需要设置 tableview 的编辑模式...

- (void)viewDidLoad{

table.editing=YES;

[super viewDidLoad];

}

希望对你有帮助 ......

于 2013-05-20T12:14:03.510 回答
0

您需要将" editingStyleForRowAtIndexPath" 方法添加到您的委托中:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return( UITableViewCellEditingStyleDelete );
}
于 2013-05-19T17:40:02.527 回答