【问题标题】:"*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'"“***由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*** - [__NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]'”
【发布时间】:2015-05-27 19:54:21
【问题描述】:

我知道这是 Stack Overflow 上的一个常见问题,但不幸的是,没有人能够引导我找到我的解决方案。

我正在尝试在UITableView 中列出我的文档目录中的文件。 (我还有另一个 tableview 显示我连接到的设备)

我知道我收到了这个错误,因为它说我的数组有 0 个对象,而我正在尝试访问索引 1 处的对象。

但我的文档目录中有一个文件。 如果我有 2 个文件,当我删除 1 个时,我会收到此错误。当应用程序重新启动时,它会正确显示 UITableView 并删除文件。

我从这里得到了我的代码: Objective-C: How to list files from documents directory into a UITableView?

这是我的内容:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _tblConnectedDevices){
    return [_arrConnectedDevices count];
    }
    if ([filePathsArray count] > 0){
        return [filePathsArray count];
    }
    else 
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _tblConnectedDevices){
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
    }

    cell.textLabel.text = [_arrConnectedDevices objectAtIndex:indexPath.row];

    return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
        NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
        NSString *last2 = [[last lastPathComponent] stringByDeletingPathExtension];
        cell.textLabel.text = last2;
        return cell;
    }
}

我的删除无效:

-(IBAction)deleteFile:(id)sender{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", fileName]];
    NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
    [_cellView reloadData];
}

【问题讨论】:

  • 这段代码中有一些非常奇怪的逻辑。 filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];在 cellForRow 中。并返回 [filePathsArray 计数];在您的行数中。你确定你明白你想要得到什么?
  • 在 deleteFile 中重新加载您的 filePathArray:同时设置断点并查看数组中有多少文件。
  • 我是相当新的@PavelGatilov,我使用的是我在网上找到的代码,它可以很好地显示我想查看的文件,我唯一苦苦挣扎的是删除后文档目录中的某些内容,表格视图在删除文件后正确重新加载。
  • 所以代码没有工作得很好。
  • @PavelGatilov 感谢您的回答,如果您将其发布为正确答案,它会起作用。我在 cellforRow 中添加了一个 if 检查我的数组是否为空,还检查文件夹是否为空。

标签: ios objective-c xcode uitableview nsdocumentdirectory


【解决方案1】:

你应该重构你的代码。然而:

我假设你在这一行得到了错误:

NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];

出了什么问题:

A.您有一个包含内存路径的数组。当表格视图询问您的项目数时,您会返回该数组的计数。

B.当表格视图连续要求一个项目时,您读取目录。 (嗯,这个方法应该没有那么快。)

filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

然后查找indexPath.row为索引的文件:

NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];

只要目录中的文件数至少与数组中的文件数一样多,此“有效”(类似于有效)。

当您从目录中删除文件时,它仍然是数组中的一个项目。因此,您对表格视图说,数组中的行数与项目数一样多。当表视图试图获取它时,您查看磁盘并找到较少的文件(已删除的文件丢失了)。因此,您从磁盘获得的数组比内存中的项目少一项。

修复它:更新您的内存数组。或者做出决定:在内存中工作在磁盘上工作。

【讨论】:

    【解决方案2】:

    在 deleteFile 中重新加载 filePathArray:同时设置断点并查看数组中有多少个文件

    【讨论】:

    • 谢谢,我在 cellforRow 中添加了一个 if 检查我的数组是否为空,同时检查文件夹是否为空以放置空白单元格文本及其工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多