2012-10-03 27 views
0

我有一個UITableView在我的UIView,有不同數量的部分,取決於一些條件。我收到下面的例外情況。NSInternalInconsistencyException表視圖中的錯誤節號?

終止應用程序由於未捕獲的異常 「NSInternalInconsistencyException」,理由是:「無效的更新:無效 節數。更新(1)後表 視圖中包含的部分數量必須等於更新(2)前表格視圖中包含的部分 的數量,加上或減去插入或刪除部分的 數目(1插入,0刪除)。

從錯誤,似乎我在數據源中有不同數量的部分? 如何解決問題?

這是我的接口:

@interface bookDetailView : SomeView <UITableViewDelegate,UITableViewDataSource> 
{ 
    UITableView *bookDetailTableView; 
     .... 
} 

這是我的看法初始化

UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease]; 
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
self.view = view; 

UITableView *tableView = [[[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped] autorelease]; 
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight); 
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
tableView.delegate = self; 
tableView.dataSource = self; 
tableView.sectionIndexMinimumDisplayRowCount = 20; 

bookDetailTableView = tableView; 
[self.view addSubview:bookDetailTableView]; 

代碼崩潰在insertSections ....

if (![book method1]) 
{ 
    [bookDetailTableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,1)] withRowAnimation:UITableViewRowAnimationFade]; 
} 
else 
{ 
    [bookDetailTableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2,1)] withRowAnimation:UITableViewRowAnimationFade]; 
} 

numberOfSectionsInTableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (editMode) 
    { 


     if(newbookFlag || listBook) 
     { 
      return 1; 
     } 
     else 
     { 
      if(![book preferable]) 
      { 
       return 2; 
      } 
      else 
      { 
       return 3; 
      } 
     } 
    } 


} 

任何想法?我嘗試了beginUpdates和endUpdates,問題仍然存在。

+0

什麼值是「numberOfSectionsinTable」和「numberOfRowsinTableView」函數返回 – AppleDelegate

+0

@AppleDelegate如何打印它?通過NSLog? – cateof

+0

是的..通過NSLog ..你可以找到@你是什麼情況下得到異常..由@Niels回答你對該部分所做的修改應該反映到'numberOfSectionsInTableView:' – Nina

回答

0

如果你插入一個節到UITableView中,你應該確保這個改變反映在你的dataSource中。在這種情況下,在插入新節時,您的numberOfSectionsInTableView:方法應該返回一個額外的行。

+0

所以代碼修改應該在numberOfSectionsInTableView? – cateof

+0

你應該在調用insertSections之前進行一些更改:以便numberOfSectionsInTableView返回插入前的節數加上要插入的節數。 – Niels

+0

有什麼變化?怎麼樣? – cateof

0

由於輕微的紅鯡魚,我實際上收到了這個錯誤。我有一個表格部分和表格單元格的模型,我們用它們來管理內存中的單元格,然後將它們呈現在屏幕上。在我的特殊情況下,我試圖使用[NSArray indexOfObject:cell]方法找到已刪除單元格的索引。由於該單元已經被從集合中移除了,而不是拋出一個異常,因此objective-c返回了int max值,導致了這個問題中出現異常,因爲我隨後根據返回的值創建了一個NSIndexPath無效。

相關問題