gpt4 book ai didi

ios - UINavigationController 与 UISearchBar 和 UISearchDisplayController

转载 作者:行者123 更新时间:2023-11-29 12:43:08 24 4
gpt4 key购买 nike

这里有几张照片可以开始:

这是我目前拥有的主视图:

MainView

然后当用户选择 UISearchBar 时,会发生以下情况:

Current

而不是第二张照片,我希望 View 更改为:

Clicked

这是目前的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

/*
self.searchView = [[UserSearchView alloc] initWithFrame:self.view.frame];
self.searchView.searchResults.delegate = self;
self.searchView.searchResults.dataSource = self;
[self.view addSubview:self.searchView];
*/

self.mainTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
[self.mainTableView setDelegate:self];
[self.mainTableView setDataSource:self];
[self.mainTableView setShowsHorizontalScrollIndicator:NO];
[self.mainTableView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:self.mainTableView];


self.searchBar = [[UISearchBar alloc] init];
[self.searchBar setBarStyle:UIBarStyleBlackTranslucent];
[self.searchBar setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

self.searchCon = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
[self.searchCon setDelegate:self];
[self.searchCon setSearchResultsDataSource:self];
[self.searchCon setSearchResultsDelegate:self];

self.navigationItem.titleView = self.searchCon.searchBar;

/* This generates the result I want but I do not want the search
* bar in the tableviews header, but rather in the uinavigationcontroller's
* uinavigationbar.
*/
//self.mainTableView.tableHeaderView = self.searchBar;

[self.view setBackgroundColor:[UIColor whiteColor]];
}

#pragma mark -
#pragma mark - UISearchDisplayController Delegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if ([searchString isEqualToString:@""])
return NO;

PFQuery *query = [PFUser query];
[query whereKey:@"username" containsString:searchString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.array = [NSMutableArray arrayWithArray:objects];
[self.searchDisplayController.searchResultsTableView reloadData];
[self.mainTableView reloadData];
} else {
[[[UIAlertView alloc] initWithTitle:@"Failed Search" message:[NSString stringWithFormat:@"%@", [error userInfo]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}];
return YES;
}


- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
[controller.searchBar setShowsCancelButton:YES animated:YES];
NSLog(@"%@", controller);
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
[controller.searchBar setShowsCancelButton:NO animated:YES];
NSLog(@"%@", controller);
}

#pragma mark -
#pragma mark - UITableView Delegate & Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

[cell.textLabel setText:[[self.array objectAtIndex:[indexPath row]] objectForKey:@"username"]];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ProfileViewController *profile = [[ProfileViewController alloc] init];
[profile setProfileUser:[self.array objectAtIndex:[indexPath row]]];
[self.navigationController pushViewController:profile animated:YES];
}

我有点不知道如何使目标 View 工作。查看 Facebook 应用程序是我想重现的,他们的搜索栏的实现方式。

此外,我正在使用 PARSE 框架。因此,很多填充到 TableView 中的数据都是从代码中抽象出来的。

最佳答案

试试这个...在你的 .h 文件中

@interface YourtViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@property (nonatomic,strong) UISearchDisplayController* searchDisplayController;
@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar; //connect this IBOutlet to your serach bar in your storyboard or xib.

在你的viewDidLoad中

- (void)viewDidLoad
{
[super viewDidLoad];
self.mySearchBar.delegate=self;
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.mySearchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;

// do your additional code here
//

}

现在实现 searchDisplayController 的委托(delegate)方法。

关于ios - UINavigationController 与 UISearchBar 和 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24378774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com