Maneesh Sharma Maneesh Sharma - 2 months ago 15
Objective-C Question

Delete multiple files from the NSDirectory

I have created an application for iOS that works like a document viewer where in I can view documents and it works like charm . I list the files in a table view and have given the swipe feature to delete the files and single file gets deleted . I have enabled the multiple selection during editing as well . The table view rows get selected , but I am not sure how to delete the selected files from the NSDirectory. Can anyone help ?

I have this in didSelectRowForIndexPath :

if (self.tableView.isEditing)
{
[_selectedRows arrayByAddingObject:[self.tableView indexPathsForSelectedRows]];
}


Ans this when delete button is pressed ,

- (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];
}

}


This is how i Populate the table view cells

- (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;


}

Answer

So because the comments are to much on the Post i just post a possible Solution.

- (void)deleteButton:(id)sender
{
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
    NSString *docPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *inboxPath = [docPath stringByAppendingPathComponent: @"/Inbox/"];

    for (NSIndexPath *indexPath in indexPaths) {
        NSString *fileName = [[_filePathsArrayCopy objectAtIndex:indexPath.row] lastPathComponent] ;
        NSString *fullFilePath= [inboxPath stringByAppendingPathComponent:fileName ];

        NSError *error;
        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:fullFilePatherror:&error];
        if (error != nil) {
            print(error)
        }
    }

}