2016-09-26 67 views
1

我已經創建了一個適用於iOS的應用程序,其工作方式類似於文檔查看器,可以在其中查看文檔,並且它的工作原理類似於魅力。我列出了表格視圖中的文件,並給出了滑動功能來刪除文件,單個文件被刪除。我也在編輯過程中啓用了多項選擇。表視圖行被選中,但我不知道如何從NSDirectory中刪除選定的文件。誰能幫忙?從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

...你不能只是抽象的方法,你用來刪除單個文件,拉所有選定的文件到一個NSArray,並循環通過調用刪除方法? –

+0

我試過了,不幸的是所有文件都被刪除。我將發佈代碼。 –

+0

請做,似乎是一個簡單的錯誤 –

回答

1

所以,因爲評論是對郵政很多我只是發佈一個可能的解決方案。

- (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) 
     } 
    } 

} 
+0

否,我發現我添加一個自定義的UILAbel到單元格的內容視圖,這就是爲什麼textLAbel.text返回null ..任何方式從該UILAbel獲取文本? –

+0

儘量不要像我在代碼中提到的那樣獲取tableViewCells,試着從你用來填充數據的列表中獲取變量sSource –

+0

我不明白這一點..你能稍微清楚一點嗎? –