0
我試圖在我的tableviewcontroller中使用搜索欄。但它沒有輸入if。在TableView中使用UISearchController
有代碼。
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
@try {
[_searchResults removeAllObjects];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS[cm] %@",searchController.searchBar.text];
NSArray *array = [_customers filteredArrayUsingPredicate:searchPredicate];
_searchResults = [array mutableCopy];
[self.tableView reloadData];
}
@catch (NSException *exception) {
NSLog(@"Error: %@",exception);
}
還有就是resultsearchController
@property (nonatomic,strong) UISearchController *resultSearchController;
而且numberofRowsinsection
方法。
if (![[self resultSearchController] isActive]) {
return _customers.count;
} else {
return _searchResults.count;
}
我也加了Cellforindexpath方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
KartList *kart;
if (_resultSearchController.active) {
kart = [_searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = kart.name;
cell.detailTextLabel.text = kart.tel1;
return cell;
} else {
kart = [_customers objectAtIndex:indexPath.row];
cell.textLabel.text = kart.name;
cell.detailTextLabel.text = kart.tel1;
return cell;
}
//Configure the cell...
}
它沒有顯示anything.So我在做什麼wrong.And我看到它不是在numberOfRowsInSection進入else子句。
感謝您的幫助。
你檢查:updateSearchResultsForSearchController?它返回正確的數組?另外,你可以發佈cellForRowAtIndexPath方法 – Miknash