【问题标题】:tableView reloadData not reloading data after deletion in documents directorytableView reloadData 在文档目录中删除后不重新加载数据
【发布时间】:2015-10-10 19:18:04
【问题描述】:

我有一个问题,我似乎无法在我的 tableView 中重新加载数据。

我想要做的是用文件目录中的文件填充我的 tableView,文件目录中带有 csv 扩展名(我已经这样做了),然后按下一个按钮,删除所有显示的文件并更新 tableView。 按下该按钮会删除文档目录中的文件,但不会使用更新的内容重新加载 tableView。

  1. 我已经使用断点来确认 reloadData 正在运行。
  2. 我尝试在运行前使用 removeAllObjects 清除我的数组 reloadData 使用 NSLog 来确认数组是空的,但是这个 不更新表格。
  3. 我尝试在运行 reloadData 之前将数组设置为 nil,但这也不起作用。
  4. 我已尝试将数组中的数据设置为 myArray = @[];前 运行 reloadData,这会用空白数据覆盖数组,但 不更新表格。
  5. 我尝试过使用这种方法 任何成功:

    dispatch_sync(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
    
  6. 我已经检查了我的代表的 tableView 并且他们出现了 正确,(tableView 中的所有其他功能都有效,包括 选择/取消选择多个项目并删除单个项目 并且每次 tableView 都会更新)。
  7. 我还搜索了多个与我类似的不同问题,但仍未找到答案。

有人可以检查我下面的方法并告诉我为什么 tableView 没有正确重新加载吗?我确定这是我在某处做错了,但我找不到它。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [filePathsArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

    _docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [_docPath objectAtIndex:0];
    _fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
    NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
    _csvFilesCell = [_fileList filteredArrayUsingPredicate:fltr];
    NSLog(@"Contents of directory: %@", _csvFilesCell);
    filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
    cell.textLabel.text = [_csvFilesCell objectAtIndex:indexPath.row];

    return cell;
}

-(IBAction) deleteStoredData:(id)sender
{

    NSLog(@"Delete data button pressed");

    UIAlertController*alert = [UIAlertController alertControllerWithTitle:@"Delete stored Data" message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* OK = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSFileManager  *manager = [NSFileManager defaultManager];

        // the preferred way to get the apps documents directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        // grab all the files in the documents dir
        NSArray *allFiles = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

        // filter the array for only csv files
        NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
        NSArray *csvFiles = [allFiles filteredArrayUsingPredicate:fltr];

        // use fast enumeration to iterate the array
        for (NSString *csvFile in csvFiles) {

            NSLog(@"PATH %@ : FILE %@", documentsDirectory, csvFile);

            NSError *error = nil;

            [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, csvFile]
                                                       error:&error];

            NSLog(@"Error: %@", error);
            NSLog(@"OK button selected, all data deleted");

            [self updateDeleteButton];

            [self.tableView reloadData];
            [alert dismissViewControllerAnimated:YES completion:nil];


        }
    }];

    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"Cancel button selected, no data deleted");
        [alert dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:OK];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];

}

【问题讨论】:

  • 它不会解决您的问题,但您可以将调用移至 for 循环之外的 reloadDatadismissViewControllerAnimated。它效率低下,可能导致未定义的行为。

标签: ios objective-c uitableview reloaddata


【解决方案1】:

看到你的代码后,我想出了以下解决方案,希望对你有帮助!!

_docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [_docPath objectAtIndex:0];
_fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
_csvFilesCell = [_fileList filteredArrayUsingPredicate:fltr];
NSLog(@"Contents of directory: %@", _csvFilesCell);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];

上面的代码应该是在点击删除按钮时触发的,而不是在tableview的data-source方法中。之后你应该重新加载你的 tableview 并且你的 cellForRowAtIndexPath 方法应该是这样的:

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

【讨论】:

  • 谢谢你的上述建议,不幸的是,这个建议没有奏效,但它确实给了我一些其他可能的解决方案的想法,但它们也没有奏效。我假设通过从 cellForRowAtIndexPath 中删除数组,您意味着它们可以在其他地方运行,因为只需完全删除它们,它会在视图加载时在表中创建一个错误。我尝试将它们实现到 viewDidLoad 中,但也没有解决问题。将上述代码添加到 deleteStoredData 方法会导致应用在按下按钮时停止工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
相关资源
最近更新 更多