2016-12-08 8 views
0

私はIOSアプリケーションを開発中です。Sqliteを使用してデータを保存しています。データはtableviewに表示され、テーブルビューから行が削除され、データはフォームデータベースsqliteを削除できます。しかし、tableviewから行を削除すると、アプリがクラッシュしています。そして、私は取得していますエラーは次のとおりです。事前にテーブルビューの行を削除し、Sqliteを使用してデータベース上のデータを削除します

Error:reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

おかげ

コード:

NSMutableArray *resultArray 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 

    UITableViewCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"cell"]; 

    dataBase *data = [_arr objectAtIndex:indexPath.row]; 

    UILabel *name = (UILabel *)[cell viewWithTag:1]; 
    name.text = data.name; 
    UILabel *department = (UILabel *)[cell viewWithTag:2]; 
    department.text = data.department; 
    UILabel *year = (UILabel *)[cell viewWithTag:3]; 
    year.text = data.year; 

    if (indexPath.row %2 ==1) //Use for color on tableview 
    cell.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1]; 
    else 
    cell.backgroundColor = [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1]; 
    return cell; 
} 
    -(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      [_tableView beginUpdates]; 

      Sqlite *sqlite = [[Sqlite alloc]init]; 
      [sqlite delete_profile:indexPath.row]; 

      NSMutableArray *newA = [[sqlite resultArray] mutableCopy]; 
      [newA removeObjectAtIndex:indexPath.row]; 

      [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
      [_tableView endUpdates]; 
     } 
     [_tableView reloadData]; 
    } 

    // Method to delete Row From Sqlite 

    -(void)delete_profile:(NSInteger)value 
    { 
     //  _categoryArray = [[NSMutableArray alloc]init]; 

     NSString *[email protected]"student.db"; // Your database Name. 

     NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); 

     NSString * DocDir=[documentpath objectAtIndex:0]; 
     NSLog(@"%@",DocDir); 
     NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename]; 


     if (sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK) 
     { 
      NSStringEncoding stringEncoding = NSUTF8StringEncoding; 
      NSString *sql = [NSString stringWithFormat:@"DELETE FROM studentsDetail WHERE regno=\"%ld\"",(long)value]; 
      const char *sqlStatement = [sql cStringUsingEncoding:stringEncoding]; 
      if (sqlite3_prepare_v2(database, 
            sqlStatement, -1, &statement, NULL) == SQLITE_OK) 
      { 
       //sqlite3_bind_int(statement, 1, value); 

       if (sqlite3_step(statement) == SQLITE_OK) 
       { 
        dataBase *base = [[dataBase alloc]init]; 
        base.name = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 
        base.department = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)]; 
        base.year = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)]; 
        [_resultArray addObject:base]; 
       } 
      } 
      else { 
       NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(database)); 
      } 
     } 
    } 
+0

私にあなたのテーブルビューDatasourseアレイ名を付け –

+0

NSMutableArrayの* resultArray –

+0

があなたの 'numberOfRowsInSection'、' numberOfSectionsInTableView'、と 'cellForRowAtIndexPath'方法を示して!試してみてください – Lion

答えて

0

[_tableView reloadData]のための必要がありません。 deleteRowsAtIndexPaths後にこの

​​
+1

'Sqlite * sqlite = [[Sqlite alloc] init];'あなたは本当ですか? – Lefteris

+0

私は彼がSqliteクラスのシングルトン・ペッテンを使用しているとは確信していません –

関連する問題