UISearchDisplayController“shouldReloadTableForSearchString 返回 NO”重新加载表
即使 shouldReloadTableForSearchString 方法返回 NO,为什么我的 UISearchDisplayController 显示“无结果”?难道它不应该什么都不做并保持黑色吗?我怎样才能阻止它这样做?
#import "RootViewController.h"
@implementation RootViewController
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 0;
}
return 10;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell.
cell.textLabel.text = [NSString stringWithFormat:@"row %d", indexPath.row];
return cell;
}
#pragma mark SearchController stuff
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (void)dealloc {
[super dealloc];
}
@end
Why does my UISearchDisplayController show "No results" even if the shouldReloadTableForSearchString method returns NO? Shouldn't it just do nothing and stay black? How can I prevent it from doing so?
#import "RootViewController.h"
@implementation RootViewController
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 0;
}
return 10;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell.
cell.textLabel.text = [NSString stringWithFormat:@"row %d", indexPath.row];
return cell;
}
#pragma mark SearchController stuff
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (void)dealloc {
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会,tableView(包括由
UISearchDisplayController
创建的searchTableView
)在显示时会自动加载数据。shouldReloadTableForSearchString:
方法将阻止仅当在搜索框中键入字符时重新加载表。请参阅我对 UISearchDisplayContoller – 可以的回答t 阻止在搜索栏中键入时重新加载表格以获取有关如何处理此问题的建议。
No, tableViews (including the
searchTableView
created by aUISearchDisplayController
) automatically load data when they are displayed. TheshouldReloadTableForSearchString:
method will prevent the table from reloading only as characters are typed into the search box.See my answer to UISearchDisplayContoller – can't prevent table reload on typing in search bar for suggestions on how to deal with this.