2011-07-11 108 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

相關問題