2017-05-08 68 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()