2016-09-26 12 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

... 1つのファイルを削除するために使用するメソッドを抽象化することはできません。選択したすべてのファイルをNSArrayにプルし、そのメソッドを呼び出してループしますか? –

+0

私はそれを試みました、そして、残念ながらすべてのファイルが削除されます。私はコードを掲載します。 –

+0

してください、簡単な間違いのようです –

答えて

1

コメントはPostにあるので、私は可能な解決策を投稿するだけです。

- (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を取得しないようにしてください。データソースを埋めるために使用したリストから変数を取得してみてください。 –

+0

私はそれを理解できませんでした。 –

関連する問題