我已经为iOS创建了一个类似于文档查看器的应用程序,在这里我可以查看文档,它就像魅力一样。我在表格视图中列出文件并给出了滑动功能以删除文件并删除单个文件。我也在编辑期间启用了多项选择。表视图行被选中,但我不确定如何从NSDirectory中删除所选文件。有人可以帮忙吗?
我在didSelectRowForIndexPath:
中有这个 if (self.tableView.isEditing)
{
[_selectedRows arrayByAddingObject:[self.tableView indexPathsForSelectedRows]];
}
按下删除按钮时回答此问题,
- (void)deleteButton:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
for (id object in _selectedRows) {
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:object];
NSString *cellText = selectedCell.textLabel.text;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"/Inbox/"];
NSString *allFilesPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *theactualFilePath = [allFilesPath stringByAppendingPathComponent:cellText];
NSError *error;
_success = [[NSFileManager defaultManager] removeItemAtPath:theactualFilePath error:&error];
}
}
这是我填充表格视图单元格
的方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"paths :%@",paths);
textlabel = [[UILabel alloc] initWithFrame:CGRectMake(15,0, 250, 70)];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"/Inbox/"];
NSString *allFilesPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if([[userDefaults objectForKey:@"searchON"] isEqualToString:@"yes"]) {
NSString *theFileName = [[_filePathsArrayCopy objectAtIndex:indexPath.row] lastPathComponent] ;
textlabel.text = theFileName;
}
else {
NSString *theFileName = [[_filePathsArray objectAtIndex:indexPath.row] lastPathComponent] ;
textlabel.text = theFileName;
}
[cell.contentView addSubview:textlabel];
return cell;
}
答案 0 :(得分:1)
因为对Post的评论非常多,我发布了一个可能的解决方案。
rac_lifetime