我正在尝试将SearchDisplayController实现为tableView。
当我创建我的tableview时,一切都可以。 我有一个带有UitableViewController委托的UiViewController。
这是我的.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface getClientsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UIView *vistaClientes;
IBOutlet UIView *menu;
IBOutlet UITableView *tablita;
}
@property(nonatomic, retain) AppDelegate *app;
@property(nonatomic, retain) NSMutableDictionary *clientList;
@property(nonatomic, retain) IBOutlet UIView *vistaClientes;
@property(nonatomic, retain) IBOutlet UIView *menu;
@property(nonatomic, retain) NSString *menuPos;
@property(nonatomic, retain) IBOutlet UITableView *tablita;
-(IBAction)showMenu:(id)sender;
@end
这是我的.m
#import "getClientsViewController.h"
#import "checkInternet.h"
#import "AccessLogin.h"
#import "ClientesCell.h"
#import "menuGlobalViewController.h"
#import "getOrdenesViewController.h"
#import "InterfaceGLobal.h"
@interface getClientsViewController ()
@end
@implementation getClientsViewController {
NSArray *recipes;
NSArray *searchResults;
}
@synthesize app,clientList,vistaClientes,menuPos,menu,tablita;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSArray *cliente = [NSArray arrayWithArray:[clientList objectForKey:@"clients"]];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"nickname contains[c] %@ OR clientNumber contains[c] %@",searchText, searchText];
searchResults = [cliente filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[clientList objectForKey:@"clients"] count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ClienteCelda";
ClientesCell *cell = (ClientesCell *)[self.tablita dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(@"Im in");
cell = [[ClientesCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray *cliente = [NSArray array];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cliente = [NSArray arrayWithArray:searchResults];
} else {
cliente = [NSArray arrayWithArray:[clientList objectForKey:@"clients"]];
}
[cell.Direccion setText:[[cliente objectAtIndex:indexPath.row] objectForKey:@"address1"]];
[cell.nombreCliente setText:[[cliente objectAtIndex:indexPath.row] objectForKey:@"name"]];
[cell.nombreCorto setText:[[cliente objectAtIndex:indexPath.row] objectForKey:@"nickname"]];
[cell.imagen setImage:[UIImage imageNamed:@"avatar.png"]];
NSLog(@"cellda: %@",[[cliente objectAtIndex:indexPath.row] objectForKey:@"address1"]);
return cell;
}
我有一些方法,只是为了使代码更加可见。
当视图加载时,正确显示结果,但是当我键入一些搜索时,返回默认样式的空单元格。
我知道,当我搜索时,if(cell == nil)为true,但未正确加载样式。
有人可以帮帮我吗?以下两个屏幕截图将有助于解释: