2013-03-23 54 views
0

當我在我的tableView中刪除一行時,我想要選中它上面的行。這不會發生......TableView不選擇刪除新行

下面是代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     if (indexPath.section == 0) { 
      Formula *toDelete = (Formula *)[self.userFRMList objectAtIndex:indexPath.row]; 
      NSManagedObjectContext *context = (NSManagedObjectContext *)[(PSAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
      NSError *error = nil; 
      [context deleteObject:toDelete]; 
      [context save:&error]; 
      [self updateDataSource]; 
      [tableView beginUpdates]; 
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      if ([self.userFRMList count] != 0) { 

       if (indexPath.row == 0) { 
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone]; 
        self.crtFormula = [self.userFRMList objectAtIndex:0]; 
        [self.delegate theFormulaIs:self.crtFormula]; 

       } else { 
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section] animated:YES scrollPosition:UITableViewScrollPositionNone]; 
        self.crtFormula = [self.userFRMList objectAtIndex:indexPath.row - 1]; 
        [self.delegate theFormulaIs:self.crtFormula]; 

       } 
      }else { 
       self.crtFormula = nil; 
       [self.delegate showBlank]; 
      } 
      [tableView endUpdates]; 
     } 

    } 

} 

回答

1

最起碼,選擇部分需要更新塊!把它放在你的endUpdates呼叫之後。

如果這樣做不行,請繼續操作:嘗試使用延遲性能做選擇部分(例如dispatch_after)。現在,您正在嘗試執行選擇,但我們仍在迴應commitEditingStyle - 我們還沒有刪除該行。延遲的性能讓接口在刪除後安定下來。

您需要使用對應的索引編號刪除後的情況。

+0

好的,我會試試看... – user1028028 2013-03-23 18:10:32

+0

工作正常,thx – user1028028 2013-03-23 18:56:09

0
// For example I have set deletedRowIndex to 10, and next Line will select Row Number 9 in tableview 
int deletedRowIndex = 10; 
[tblViewList selectRowAtIndexPath:[NSIndexPath indexPathWithIndex:deletedRowIndex-1] animated:YES scrollPosition:UITableViewScrollPositionMiddle];