简体   繁体   English

从文档目录中删除所有文件后,UITableView无法重新加载

[英]UITableView is not reloading after I delete all files from Document Directory

Quick help here please:-) 请在这里快速帮助:-)

I have populated a UITableView with the contents of my Documents Directory and I can delete all the files with one button from the directory. 我已经用文档目录的内容填充了UITableView,并且可以通过一个按钮从目录中删除所有文件。

Now when I have deleted the files, the list of what the user sees still is the same and not until I reload the view will it show that indeed those files are gone. 现在,当我删除文件时,用户看到的列表仍然是相同的,并且直到我重新加载视图后,它才会显示这些文件确实消失了。

I have used as per my code below: 我已经按照下面的代码使用了:

[_mainTableView reloadData];
[_mainTableView beginUpdates]; & [_mainTableView endUpdates];

None of those are working, but I need the tableView to refresh once those files are gone. 这些都不起作用,但是一旦这些文件消失了,我就需要tableView刷新。

        [_mainTableView beginUpdates];            
        NSFileManager *fileMgr = [[NSFileManager alloc] init];
        NSError *error = nil;

        NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:[self filePath] error:&error];
        NSLog(@"FILES - %@", directoryContents);

       if (error == nil) {
            for (NSString *path in _mainDataArray) {
                NSString *fullPath = [[self filePath] stringByAppendingPathComponent:path];
                BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
                if (!removeSuccess) {
                    // Error handling

                }
                else if(removeSuccess)
                {
                    [_mainTableView reloadData];
                }
            }
        }
        [_mainTableView endUpdates];

Any other things I need to try?? 我还需要尝试其他吗?

I think you don't need to create a new instance of the NSFileManager ; 我认为您不需要创建NSFileManager的新实例; instead use: 改为使用:

[NSFileManager defaultManager];

The first if condition if (error == nil) will be true as error is nil and so I don't think it is going inside the for loop 第一个if条件if (error == nil)将为true,因为错误为nil,所以我不认为它会在for循环内进行

You're not emptying the array that populates your table. 您不会清空用于填充表格的数组。 You're using the data in the array to delete items in the folder, but not removing items from the array, so, of course, the table is still populated. 您正在使用数组中的数据删除文件夹中的项目,但未从数组中删除项目,因此,当然,表仍在填充中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM