ios - tableView reloadData 在文档目录中删除后不重新加载数据

标签 ios objective-c uitableview reloaddata

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

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

  • 我已经使用断点来确认 reloadData 正在运行。
  • 我曾尝试在运行前使用 removeAllObjects 清除我的数组
    reloadData 使用 NSLog 来确认数组是空的,但是这个
    不更新表。
  • 在运行 reloadData 之前,我尝试将数组设置为 nil,这也不起作用。
  • 我尝试将数组中的数据设置为 myArray = @[];前
    运行 reloadData,这会用空白数据覆盖数组,但
    不更新表。
  • 我试过使用这种方法没有
    任何成功:
    dispatch_sync(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
    
  • 我已经检查了我的代表的 tableView 并且他们出现了
    正确,(tableView 中的所有其他功能都有效,包括
    选择/取消选择多个项目并删除单个项目
    并且每次tableView都会更新)。
  • 我还搜索了多个与我类似的不同问题,但仍然没有找到答案。

  • 有人可以检查我下面的方法并告诉我为什么 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];
    
    }
    

    最佳答案

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

    _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;
        }
    

    关于ios - tableView reloadData 在文档目录中删除后不重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31537309/

    相关文章:

    ios - 更改字体大小以适合 UITextView

    iphone - 捕获包含状态栏的 iPhone 屏幕?

    ios - -[NSInputStream 读取 :maxLength:] throws an exception saying length is too big, 但它不是

    swift - 快速更新 firebase 数据库的特定部分

    ios - Swift:无法在自定义 View 上调用 removeFromSuperview()

    ios - Urban Airship 和AirshipConfig.plist

    objective-c - 如何组织 "Window"菜单?

    ios - 如何在自定义tableview单元类中使用UITableviewcell的dragStateDidChange方法

    ios - 如何在 ios 的表格 View 中设置多色文本

    objective-c - CABasic 完成动画后不删除图像