2017-05-08 43 views
0

因爲我刪除了json數據。每當我刪除json數據時,我必須刪除tableview中的特定數據。此外,無論何時我試圖刪除第一行中的數據,它將引發異常,如「終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'嘗試刪除第1行從第0節,其中只包含1行更新'」和應用程序崩潰Tableview重新加載數據不工作在uitableview

任何人都可以建議我在以下編碼中出現了什麼問題?

- (IBAction)deletebutton:(id)sender { 
    NSString *userid = [[NSUserDefaults standardUserDefaults] 
         stringForKey:@"UserId"]; 
    NSString *encodeddistrictvalue = [selecteddistrictvalue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]; 
    NSString *encodedPanchaytvalue = [selectedpanchayatvalue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]; 
     NSString *urlStr = [NSString stringWithFormat:@"my json url",encodeddistrictvalue,encodedPanchaytvalue,taxtypevariable,selctedassessmentno,userid];NSURL *url = [NSURL URLWithString:urlStr]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, 
               NSData *data, NSError *connectionError) 
    { 
      if (data.length > 0 && connectionError == nil) 
      { 
       NSDictionary *dictionaryvalue = [NSJSONSerialization JSONObjectWithData:data 
                     options:0 
                     error:NULL]; 
       NSString *code = [[dictionaryvalue objectForKey:@"code"] stringValue]; 
       NSLog(@"the response string value is %@",code); 
       if ([code intValue]==1) { 
        NSLog(@"deletd sucessfully"); 
       UIAlertController * alert = [UIAlertController 
               alertControllerWithTitle:@"Alert" 
               message:@"Are u sure u want to delete the table data" 
               preferredStyle:UIAlertControllerStyleAlert]; 
        UIAlertAction* yesButton = [UIAlertAction 
               actionWithTitle:@"yes" 
               style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action) { 
               { 
                [assessmentnumber removeObject:selctedassessmentno ]; 
                [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"expandingCell"]; 
                [_tableview reloadData]; 
               } 
                }]; 
        [alert addAction:yesButton]; 
        [self presentViewController:alert animated:YES completion:nil]; 
        alert.view.tintColor = [UIColor blackColor]; 

       } 
      } 


    } ]; 

    } 
+0

太多的代碼。請將您的問題編輯爲可理解的數量。 – brimstone

+0

由於不便,先生.i編輯我的問題請檢查 – user19

+0

不要重新加載整個表格視圖。使用'deleteRowsAtIndexPaths'。如果您使用的是故事板和原型單元,則根本不需要註冊單元。 – vadian

回答

0

把這個viewDidLoad中

[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"expandingCell"]; 
+0

感謝您的回覆,如果我把它放在viewdidload我收到以下異常「終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [UITableViewCell numberlabel]:無法識別的選擇器發送到實例0x145c1f640'「 – user19

+0

數組可能不會更改。 –

-1

您可以使用開始&端更新:提供

Table.beginUpdates() 
    for i in 0 ..< sections[section].items.count 
    { 
     Table.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic) 
    } 
    Table.endUpdates()