gpt4 book ai didi

ios - "*** Terminating app due to uncaught exception ' NSRangeException ', reason: ' *** -[__NSArrayM objectAtIndex :]: index 1 beyond bounds [0 . 。 0] '"

转载 作者:行者123 更新时间:2023-11-29 02:11:38 25 4
gpt4 key购买 nike

我知道这是 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];
}

最佳答案

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

关于ios - "*** Terminating app due to uncaught exception ' NSRangeException ', reason: ' *** -[__NSArrayM objectAtIndex :]: index 1 beyond bounds [0 . 。 0] '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29233736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com