Search code examples
iosobjective-cuitableviewnsfilemanager

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.

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.

        [_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??


Solution

  • 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.