【发布时间】:2015-11-25 00:04:59
【问题描述】:
我一直在尝试通过关注stackoverflow线程来实现UISearchController:
How to implement UISearchController with objective c
和 Apples 的文档,但无法使其正常工作。当初始控制器(EVTSearchViewController 定义为UIViewController)符合UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate
(我也需要使其与UITableViewDelegate 兼容,因为我使它成为其他UITableViewController 类型EVTSearchResultsViewController *resultsControllers tableView 的代表)代表出现了,我看到我的.xib 带有UISearchBar 和@ 987654336@,我点击搜索栏开始输入:
当我输入一个字母时,搜索栏消失并且什么也没有显示:
首先,我不希望搜索栏消失。其次,updateSearchResultsForSearchController 似乎根本没有被调用,因为NSLog() 在那里设置在我在搜索栏中输入时不会产生任何输出。
我有.xib 用于EVTSearchViewController,它有一个UISearchBar,我正在连接到相应的属性:IBOutlet UISearchBar *searchBar,然后指向 UISearchControllers 的搜索栏:
self.searchBar = self.searchController.searchBar
还有UITableView,我在.xib 中放在UISearchBar 下方。我在EVTSearchViewController 中使用的另一个控制器是EVTSearchResultsViewController,它是UITableViewController,它没有.xib。
下面是viewDidLoad 和updateSearchResultsForSearchController 方法的代码:
- (void)viewDidLoad
{
_resultsController = [[EVTSearchResultsViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
self.searchBar = self.searchController.searchBar;
// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// update the filtered array based on the search text
NSString *searchText = searchController.searchBar.text;
NSLog(@"searchText: %@", searchText);
if (searchText == nil) {
// If empty the search results are the same as the original data
self.searchResults = [[[EVTItemStore sharedStore] allItems] mutableCopy];
} else {
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
NSArray *allEvents = [[EVTItemStore sharedStore] allItems];
NSLog(@"allEvents: %@", allEvents);
for (EVTItem *event in allEvents) {
/*if ([event.number containsString:searchText] || [[phoneMO.closrr_id filteredId] containsString:searchText] || [[phoneMO.contact.fullname lowercaseString] containsString:[searchText lowercaseString]]) {
[searchResults addObject:phoneMO];
}*/
if ([event.eventName containsString:searchText]) {
[searchResults addObject:event];
}
}
self.searchResults = searchResults;
}
// hand over the filtered results to our search results table
EVTSearchResultsViewController *resultsController = (EVTSearchResultsViewController *)self.searchController.searchResultsController;
resultsController.filteredEvents = self.searchResults;
[resultsController.tableView reloadData];
}
EVTSearchViewController中定义的各个@properties:
@interface EVTSearchViewController ()
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) EVTSearchResultsViewController *resultsController;
@property (nonatomic, strong) NSMutableArray *searchResults;
// For state restoration
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;
@end
那么,这里是EVTSearchResultsViewController的代码:
#import "EVTSearchResultsViewController.h"
@implementation EVTSearchResultsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"UISearchViewCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.filteredEvents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"UISearchViewCell"
forIndexPath:indexPath];
cell.textLabel.text = self.filteredEvents[indexPath.row];
return cell;
}
@end
上面EVTSearchResultsViewController的方法根本没有被调用,这让我看起来很奇怪,那我们为什么需要它呢?
我尝试按照 Apple 文档推荐的方式设置 UISearchBar:
self.resultsTableView.tableHeaderView = self.searchController.searchBar;
但它给了我一个无响应的搜索框,所以当按下时没有任何反应。
有人可以帮忙解决这个问题吗?上面链接的另一个问题也可以澄清。谢谢。
【问题讨论】:
-
您的实现似乎过于复杂。您通常希望将 uisearchbar 添加到要搜索的 tableview 中,而不是分布在多个类和 xib 中。尝试从一个非常简单的示例从头开始,直到您了解编码模式。
-
好吧,我相信所有的事情都是必需的,应该以这种方式(或类似方式)完成,再简单不过了。如果你看一下苹果文档,它已经很复杂了:developer.apple.com/library/ios/documentation/UIKit/Reference/…
-
现在调用这些方法,所以我更好地了解工作流程。我现在有两个主要问题:当一个视图覆盖另一个视图时,图像在上面表示的一个问题;另一个是
updateSearchResultsForSearchController中的NSString *searchText = searchController.searchBar.text在我输入文本时没有设置。 -
我现在遇到的主要问题是上面显示的问题,当一个视图运行在另一个视图之上时。我解决了设置文本的问题(我只看到正确的 NSLog() 输出)
-
您应该使用文档中的这一行:self.tableView.tableHeaderView = self.searchController.searchBar;正如我在第一条评论中建议的那样,请参阅您发送给我的链接以获取更多详细信息。
标签: ios objective-c iphone uitableview cocoa-touch