2013-04-08 59 views
0
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(editingStyle == UITableViewCellEditingStyleDelete) { 

     //Get the object to delete from the array. 
     NSLog(@"%@",[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]]); 

     NSInteger myInteger = [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue]; 
     NSNumber *selRow = [NSNumber numberWithInt:myInteger]; 
     NSLog(@"%@",selRow); 
     [self removeCell:selRow]; // this is working perfectly it gets remove from database 

     //Delete the object from the table. 
     // app get crash here crash report is pested below 
     [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 
} 

- (void) removeCell:(NSNumber *)selRow{ 

     NSLog(@"_arrayForTable%@",_arrayForTable); 
NSInteger myInteger = [selRow integerValue]; 

    [_arrayForTable removeObjectAtIndex:myInteger]; 

    NSLog(@"_arrayForTable%@",_arrayForTable); 



    if(deleteStmt == nil) { 
     const char *sql = "delete from PersonNamesAndBirthDates where ID = ?"; 
     NSLog(@"%s",sql); 
     if(sqlite3_prepare_v2(database1, sql, -1, &deleteStmt, NULL) != SQLITE_OK) 
      NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database1)); 
    } 
    NSLog(@"%d",myInteger); 

    //When binding parameters, index starts from 1 and not zero. 
    sqlite3_bind_int(deleteStmt, 1, myInteger); 

    if (SQLITE_DONE != sqlite3_step(deleteStmt)) 
     NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database1)); 

    sqlite3_reset(deleteStmt); 
} 

*在聲明失敗 - [UITableView的_endCellAnimationsWithContext:],2013年4月8日/SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 17:01 :13.069 birthdate reminder 2 [583:15803] *終止應用到期 未捕獲的異常'NSInternalInconsistencyException',原因: '無效的更新:無效的行數在第0部分中.行中包含的現有節更新(12)必須是 等於該部分中包含的行數 更新(12),加或減從 插入或刪除的行數(0插入,1刪除)以及加或減行數 移入或移出該部分的行數(0移入,0移入出)。」碰撞從表視圖除去細胞

請建議一些

我numberofrow方法如下

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 


    return _arrayForTable.count; 

} 

應用程序不崩潰每次某個作品的某個時候崩潰

+1

粘貼numnerOfRows方法 – 2013-04-08 11:34:34

+0

確定馬諾哈爾加入 – 2013-04-08 11:38:29

+0

我的猜測是,你對'removeCell假設:?',這是工作這個行可能會從你的數據庫中刪除,但是你有沒有檢查你的變量'_arrayForTable'?看起來,它仍然存在。 – fguchelaar 2013-04-08 11:52:31

回答

1

好了,因爲我看到它,你得到一個特定行的ID有:

NSInteger myInteger = 
    [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue]; 
NSNumber *selRow = [NSNumber numberWithInt:myInteger]; 

然後,您使用的特定行的ID,因爲索引中刪除來自陣列。

NSInteger myInteger = [selRow integerValue]; 
[_arrayForTable removeObjectAtIndex:myInteger]; 

這就是事情出錯的地方。如果數組與您的TableView具有相同的順序,則需要從中刪除indexPath.row。該ID對於您的數據庫是必需的。

+0

是的,我得到它。我想發佈答案,但我做nt有點非常感謝你:) – 2013-04-08 12:11:05

0

試試這個:

- (void)tableView:(UITableView *)tv commitEditingStyle :(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(editingStyle == UITableViewCellEditingStyleDelete) { 

     //Get the object to delete from the array. 
     NSLog(@"%@",[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]]); 

     NSInteger myInteger = [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue]; 
     NSNumber *selRow = [NSNumber numberWithInt:myInteger]; 
     NSLog(@"%@",selRow); 

     [self.table beginUpdates];   

     [self removeCell:selRow]; // this is working perfectly it gets remove from database 

     //Delete the object from the table. 
     // app get crash here crash report is pested below 
     [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     [self.table endUpdates]; 

    } 
} 

注意的[self.table beginUpdates];[self.table endUpdates];圍繞改變tableview的代碼行。

0

IT「S工作 你commitEditingStyle方法,並加入這一行

[TableViewForFirstView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];