2011-07-11 13 views
3

asihttprequestを使用してファイルをダウンロードしますが、ダウンロードが失敗すると、テーブル内のファイルに関連付けられたセルはテーブルビューから削除されません。テーブルからセルを削除する方法

私は削除するスワイプすると、私はこのエラーを持っている:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'

どのようにして、セルを削除することができますか?

MANAGER.h

NSMutableArray *contentArray; 

MANAGER.m

- (id)initWithStyle:(UITableViewStyle)style { 
if (self == [super initWithStyle:style]) { 

    self.navigationController.navigationBar.tintColor = [UIColor blackColor]; 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    contentArray = [[NSMutableArray alloc] init]; 
    progArray = [[NSMutableArray alloc] init]; 
    self.view.backgroundColor = [UIColor clearColor]; 

    //saveTo = [[NSUserDefaults standardUserDefaults] stringForKey:@"save_to"]; 

    AppDelegate_iPhone* delegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 
    if([delegate saveTo] != nil) 
    { 
     saveTo = [delegate saveTo]; 
    } 

    if(saveTo == nil) 
    { 
     saveTo = @"/"; 
    } 


    allFiles = [[NSMutableArray alloc] init]; 
    NSDirectoryEnumerator *dirEnum = [ [ NSFileManager defaultManager ] enumeratorAtPath:saveTo]; 
    NSString *file; 
    while ((file = [ dirEnum nextObject ])) 
    { 
     if(file != nil) 
     { 
      if([file rangeOfString:@".mp4" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mov" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".m4v" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".pdf" options: NSCaseInsensitiveSearch].length > 0) 

      { 
       [ allFiles addObject: file]; 
      } 
     } 
    } 
} 
return self; 
} 

    // Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(indexPath.section == 0) 
    { 

     if (editingStyle == UITableViewCellEditingStyleDelete) 
     { 
      Selected = indexPath.row; 
      //Remove The Temporary File 
      NSFileManager *fileManager = [NSFileManager defaultManager]; 
      if([fileManager fileExistsAtPath:[(ASIHTTPRequest *)[contentArray objectAtIndex:indexPath.row] temporaryFileDownloadPath]]) 
      { 
...... 


- (void)requestFailed:(ASIHTTPRequest *)request 
    { 

     [contentArray removeObject:request]; 
     [progArray removeObject:request]; 
     [self reloadMyData]; 
     [self.tableView reloadData]; 
    } 

-(void)reloadMyData 
{ 
    allFiles = [[NSMutableArray alloc] init]; 
    AppDelegate_iPhone* delegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 
    if([delegate saveTo] != nil) 
    { 
     saveTo = [delegate saveTo]; 
    } 

    NSDirectoryEnumerator *dirEnum = [ [ NSFileManager defaultManager ] enumeratorAtPath:saveTo]; 
    NSString *file; 
    while ((file = [ dirEnum nextObject ])) 
    { 
     if(file != nil) 
     { 
      //if([file rangeOfString:@".mp4" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mov" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mp3" options: NSCaseInsensitiveSearch].length > 0) 
      if([file rangeOfString:@".mp4" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".mov" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".m4v" options: NSCaseInsensitiveSearch].length > 0 || [file rangeOfString:@".pdf" options: NSCaseInsensitiveSearch].length > 0) 
      { 
       [ allFiles addObject: file]; 
      } 
     } 
    } 


} 



#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { 
    // Return the number of sections. 
    return 2; 
} 


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if(section == 0) 
    { 
     return [progArray count]; 
    } 
    else { 
     return [allFiles count]; 
    } 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *CellIdentifier = [NSString stringWithFormat:@"CellIdentifier_%i_%i",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = nil; 

    if(indexPath.section == 0) 
    { 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
     [cell addSubview:[progArray objectAtIndex:indexPath.row]]; 
    } 
    else { 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     // cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
     cell.textLabel.text = [allFiles objectAtIndex:indexPath.row]; 
    } 


    return cell; 
} 
+0

削除しようとしているオブジェクトがデータソースにありません – JoBu1324

+0

ダウンロードが失敗しました[contentArray removeObject:request];セルを削除して削除すると、オブジェクトがすでに削除されているため、エラーが発生します。ダウンロードが失敗した直後にセルを削除するにはどうすればよいですか? – Alby

+0

ダウンロードが失敗したときにオブジェクトを削除しないと、アプリケーションがクラッシュしますか?そうでない場合、スワイプを実行してセルを削除して削除すると、オブジェクトは適切に削除されますか? – JoBu1324

答えて

5

のtableViewから行を削除するときは、同時にデータソースからオブジェクトを削除する必要があります行を削除します。データソースから行を削除する場合は、削除時にテーブル(-deleteRowsAtIndexPaths:withRowAnimation :)を更新して、tableViewとデータソースの関係を維持する必要があります。

+0

インデックスパスに5つのオブジェクトがあり、3番目のオブジェクト(たとえば)が必要な場合、どうすればよいですか?ダウンロードが失敗した後に削除されますか? – Alby

+0

ユーザー操作を必要とせずに関連するREQUESTの行を削除する可能性はありますか? – Alby

+0

はい、上記の方法はメモリからのものでしたが、それほど正しくはありませんでした。ユーザーの操作なしで行を削除するには、データソースから行を削除した後にtableView *に-deleteRowsAtIndexPaths:withRowAnimation:メッセージを送信する必要があります。次に、[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:section]] withRowAnimation:YES]の例を示します。 – JoBu1324

関連する問題