2011-07-11 109 views
3

我使用asihttprequest来下载文件,但是当下载失败时,表格中与文件关联的单元格不会从tableview中删除。如何从表格中删除单元格

当我滑动删除,我有这样的错误:

* 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个对象在索引路径和第三个(例如)需要下载失败后删除? – Alby

+0

是否有机会在不需要用户交互的情况下移除相关REQUEST的行? – Alby

+0

是的,虽然我上面提到的方法来自记忆,但它不太正确。要在没有用户交互的情况下删除行,您需要在删除数据源中的行后将-deleteRowsAtIndexPaths:withRowAnimation:消息发送到tableView *。下面是一个例子:[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:section]] withRowAnimation:YES]。 – JoBu1324

相关问题