![](/img/trans.png)
[英]Why am I getting an NSInternalInconsistencyException exception on my UITableView?
[英]Why am I getting an NSInternalInconsistencyException after my second UITableView search?
我了解在UITableViewDataSource協議下,我必須提供每個節的行數。 在啟用了FMDB SQL的應用程序中,當用戶執行表搜索時,我將返回此值,但是如果取消搜索然后重新執行搜索,則先前的行計數仍然存在,但是沒有文本的新搜索為0,從而導致不一致異常。 為了解決這個問題,我在取消之前緩存了計數值,但這給了我相反的矛盾。 是否應在取消時將該行計數顯式重置為零?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (self.isSearching){
if(self.searchReset){
self.searchReset = NO;
NSLog(@"Search reset prev row count: %ld", (long)self.prevSearchRowCount);
return self.prevSearchRowCount;
}
NSLog(@"Title Count From Match: %ld", (long)[self getTitleCountWhereTitleMatch]);
return [self getTitleCountWhereTitleMatch];
}else{
NSLog(@"Title Count %ld", (long)[self getTitleCount]);
return [self getTitleCount];
}
}
-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
NSLog(@"Cancel clicked or did end search");
self.isSearching = NO;
self.searchReset = YES;
self.prevSearchRowCount = [self.searchDisplayController.searchResultsTableView numberOfRowsInSection:0];
[self.searchTitles removeAllObjects];
}
*由於未捕獲的異常'NSInternalInconsistencyException'而終止應用程序,原因:'無效的更新:第0節中的行數無效。更新(14)之后現有節中包含的行數必須等於該段中包含的行數更新前的那個部分(2),加上或減去從該部分中插入或刪除的行數(插入的0,刪除0)和加上或減去移入或移出該部分的行數(移入的0,0搬出)。
確保已設置searchDisplayController的委托。
///Init UISearchDisplayController
_mySearchDisplayController=[[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
_mySearchDisplayController.delegate=self;
_mySearchDisplayController.searchResultsDataSource=self;
_mySearchDisplayController.searchResultsDelegate=self;
使用tableView == self.searchDisplayController.searchResultsTableView
判斷是否處於搜索模式。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( tableView == self.searchDisplayController.searchResultsTableView )
{
return _arrayFilteredAudioFiles.count;
}
else
{
return _arrayAudioFiles.count ;
}
}
當您更新(插入或刪除)單元格時。 在Tableview嘗試重新加載數據之前,請確保首先更新您的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.