【发布时间】:2014-12-20 02:14:29
【问题描述】:
所以我的问题是当我在搜索栏上键入文本时,我无法在表格视图中显示项目。
我很确定除了输入搜索栏时显示的项目外,一切正常。 当我使用 NSLog 时,我可以看到: - 具有从客户端数组中提取的项目的 filterClients - 我可以看到 cellForRowAtIndexPath 处的单元格不为空。
我不知道为什么它没有显示在 tableview 中。
#import <UIKit/UIKit.h>
@interface CRMClientTableVC : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>
@property (strong, nonatomic) NSMutableArray *filterdClients;
@end
#import "CRMClientTableVC.h"
#import "AppDelegate.h"
#import "CRMClientTableViewCell.h"
#import "CRMClientProfileViewController.h"
@interface CRMClientTableVC (){
NSArray *clients;
}
@end
@implementation CRMClientTableVC{
id <CRMDataManager> _dataManagerInterface;
}
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
_dataManagerInterface = appDelegate.dataManager;
clients = [_dataManagerInterface retrieveClients];
self.filterdClients = [[NSMutableArray alloc] initWithCapacity:clients.count];}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if(tableView == self.searchDisplayController.searchResultsTableView){
return [self.filterdClients count];
}else{
return [clients count];
}
}
//SOMETHING DOES NOT WORKKK HERE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Calling table view");
CRMClientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClientTableViewCell"];
if (!cell) {
cell = [[CRMClientTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ClientTableViewCell"];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
CRMClient *client = [self.filterdClients objectAtIndex:indexPath.row];
[cell populateData:client];
NSLog(@"Yeahhhhhh");
NSLog(@"%@", self.filterdClients);
NSLog(@"%@", cell);
}else{
CRMClient *client = [clients objectAtIndex:indexPath.row];
[cell populateData:client];
NSLog(@"nahhhhhhhh");
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// reset the cell selected
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// get selected index to get client data
CRMClient *selectedClient = [clients objectAtIndex:indexPath.row];
// pass client data to profile view
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CRMClientProfileViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"CRMClientProfileViewController"];
viewController.client = selectedClient;
[[self navigationController] pushViewController:viewController animated:YES];
}
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
NSLog(@"%@", searchText);
[self.filterdClients removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
self.filterdClients = [NSMutableArray arrayWithArray:[clients filteredArrayUsingPredicate:predicate]];
// NSLog(@"%@", self.filterdClients);
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
这是当我键入“Bob ta”时输出中出现的内容,它是客户列表的一个对象项。
2014-10-24 15:41:49.574 Insurance_CRM[7548:159250] Bob tan
2014-10-24 15:41:49.575 Insurance_CRM[7548:159250] Calling table view
2014-10-24 15:41:49.576 Insurance_CRM[7548:159250] Yeahhhhhh
2014-10-24 15:41:49.576 Insurance_CRM[7548:159250] (
"<CRMClient: 0x7fe8a0f7c8a0>"
)
2014-10-24 15:41:49.576 Insurance_CRM[7548:159250] <CRMClientTableViewCell: 0x7fe8a0d70ec0; baseClass = UITableViewCell; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7fe8a0d70dd0>>
【问题讨论】:
标签: ios uitableview storyboard uisearchbar