0
刪除UITableView部分的最後一行時出現此錯誤。ios6刪除導致錯誤的部分中的最後一個單元格
***由於未捕獲異常'NSInternalInconsistencyException',原因:'無效更新:節數無效。更新後的表視圖中包含的段數(2)必須等於更新前的表視圖中包含的段數(3),加上或減去插入或刪除的段數(0插入,0刪除)。」
我的表格有3個部分如果一個數組的計數大於0(用戶可以自己添加位置)。如果數組等於0,那麼該表只包含2個部分,這是我認爲問題所在的地方,我無法弄清楚。
我有刪除的細胞和更新數組的代碼是:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* delCell = [tableView cellForRowAtIndexPath:indexPath];
delName = delCell.textLabel.text;
[self deleteLocation];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
-(void)deleteLocation {
NSLog(@"Name = %@",delName);
NSUserDefaults *customLocations = [NSUserDefaults standardUserDefaults];
NSUserDefaults *sharedPref = [NSUserDefaults standardUserDefaults];
[customLocations removeObjectForKey:delName];
[customLocations synchronize];
[customLoc removeObject:delName];
saveCustomLoc = [NSArray arrayWithArray:customLoc];
[sharedPref setObject:saveCustomLoc forKey:@"CustomLocations"];
[sharedPref synchronize];
NSLog(@"%@",saveCustomLoc);
NSLog(@"%lu",(unsigned long)[saveCustomLoc count]);
[self showAlert];
}
- (void) showAlert {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Location Deleted"
message:@"The location has been deleted"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
,直到最終細胞的偉大工程。
的我的表是如何設置的一個例子是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
if ([customLoc count] == 0) { // If there is no data in the custom locations array the don't include that section
if (section == 0) {
return [serverSelection count];
}
else {
return [qServerSelection count];
}
}
else {
if (section == 0) {
return [customLoc count];
}
else if (section == 1) {
return [serverSelection count];
}
else {
return [qServerSelection count];
}
}
所以標題/行數/段等的數目是依賴於一個陣列的內容。事實上,細胞正在從消失中刪除(或者突然有4個細胞在第1部分移動到第0部分),我認爲是造成這個問題。
任何想法如何解決這個問題?
您可以擁有0行的部分。你的問題是由你自己的代碼不一致造成的。如果你的'commitEditingStyle'方法簡單地從表(和數據源)刪除最後一行,那麼你的'numberOfRowsInSection'也必須返回0。但是如果有0行,你的'numberOfSections'和'numberOfRowsInSection'就會忽略整個部分。這是造成不一致性錯誤的原因。 – rmaddy 2013-03-17 21:51:43
感謝澄清@rmaddy! – 2013-03-18 18:24:18