再会,
我正在使用UITableViewController来显示Search Items。
我的代码如下: 问题是,当我在viewDidLoad中调用GETSEARCH函数时,它会运行并执行回调TITLEITEMSRETURNED。并且tableView正确重新加载。
但是,如果我使用搜索栏并执行 GETSEARCH。委托被调用,数据正确加载到数组中,但 tableView 永远不会更新。
但是,如果我按下灰色的十字按钮,表格会突然更新!!?是什么赋予了?
-(void)TitleItemsReturned:(NSArray*)titleItems{
for(TitleItem* titleItem in titleItems){
// NSLog(@"TITLE: %@ ISBN: %@",titleItem.Title,titleItem.ISBN);
[searchResults addObject:titleItem];
}
[self.tableView reloadData];
}
- (void)viewDidLoad
{
NSLog(@"RUN");
networkLayer=[[NLBNetworkLayer alloc]init];
searchResults=[[NSMutableArray alloc]initWithCapacity:500];
// [networkLayer getBookSearch:TITLE term:@"Inferno"];
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated{
[networkLayer setDelegate:(id)self];
}
#pragma mark - Table view data source
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
TitleItem *titleItem = nil;
titleItem = [searchResults objectAtIndex:indexPath.row];
// Configure the cell
cell.textLabel.text = titleItem.Title;
NSLog(@"called %@",titleItem.Title);
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"count %d",[searchResults count]);
return [searchResults count];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
//[networkLayer getBookSearch:TITLE term:searchBar.text];
[networkLayer getBookSearch:TITLE term:@"Inferno"];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
NSLog(@"all removed");
[searchResults removeAllObjects];
[self.tableView reloadData];
}