![](/img/trans.png)
[英]What to do when error thrown in tableView(_:numberOfRowsInSection:)?
[英]tableView:numberOfRowsInSection error when searching
我正在構建一個iOS應用程序,其中有車輛庫存編號列表,我需要搜索特定的庫存編號。
我已經為UITableView實現了必需的方法,並設置了委托和源,等等-我的tableview出現並可以正常工作。
奇怪的是,當我在頂部的搜索框中鍵入任何內容(由“搜索欄和搜索顯示控制器”預制的對象提供)時,應用程序崩潰:
*由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'-[[CarLocateViewController tableView:numberOfRowsInSection:]:無法識別的選擇器已發送到實例0x757d340'
這很奇怪,因為我已經實現了tableView:numerOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"Defining rows for table view: %i", allVehicles.count);
return allVehicles.count;
}
這對於單獨使用表格視圖非常有效。
我還設置了搜索方法:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredVehicles removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
filteredVehicles = [NSMutableArray arrayWithArray:[allVehicles filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
任何幫助表示贊賞!
更改評論以回答:
這可能是一個愚蠢的問題,但是您的tableview數據源和委托以及CarLocateViewController對象內的必需函數是嗎? 您提供的代碼並未完全說明這些方法的位置。
您分配給DataSource
的類實例未實現UITableViewDataSource
函數。
@interface MyClassController :UIViewController <UITableViewDelegate,UITableViewDataSource>
嘗試如下。
myTableView.dataSource = self;
myTableView. delegate = self;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.