美好的一天,
我正在使用UITableViewController来显示搜索项.
我的代码如下:问题是,当我在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];
}
Run Code Online (Sandbox Code Playgroud)
确保reloadData从主线程发送消息,否则可能会出现问题.看起来该TitleItemsReturned方法可能不是从主线程调用的(例如,从对象NSURLConnectionDelegate实现的方法中的后台线程networkLayer,或类似的委托方法).
如果TitleItemsReturned确实没有在主线程上运行,你可以在以下内容中执行TitleItemsReturned:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
Run Code Online (Sandbox Code Playgroud)
该searchBarCancelButtonClicked方法正在运行,因为该方法在主线程上运行(来自UI事件).
| 归档时间: |
|
| 查看次数: |
1312 次 |
| 最近记录: |