在我的应用程序中,它有两个 View 。在第一个 ViewController
有一个tableview
.当第一个单元格从tableview
中选择时,它会加载另一个 View 和另一个 tableview
使用搜索选项。然后从 tableview
中的搜索结果中获取,在我选择一个单元格后,当前 View 应该关闭。
viewcontroller
这是我的代码
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *enteredString = searchController.searchBar.text;
if ([enteredString length] >= 3) {
[self getAirportCodesFromWebService:enteredString];
}
[self.airportTable reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [airportArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GetAirportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
GetAirport *getAirports = [airportArray objectAtIndex:indexPath.row];
cell.AirportName.text = getAirports.AirportName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
GetAirport *getAirports = [airportArray objectAtIndex:indexPath.row];
NSString *selectedAirport = getAirports.AirportName;
[[NSUserDefaults standardUserDefaults] setObject:selectedAirport forKey:@"selectedairport"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
我用了
UISearchController
以编程方式为 searh.inside 我根据用户输入调用 web 服务。帮我解决这个问题
最佳答案
首先,在您的第二个 View Controller 中,您有一个带有搜索栏的表格 View 。
当您尝试在搜索栏中搜索任何内容时,搜索栏有自己的表格 View 。
希望您能正确区分 tableview 委托(delegate)和数据源方法。
从搜索 tableview 中选择任何单元格时,您应该调用
[self.searchDisplayController setActive:NO animated:YES];
这将关闭您的搜索结果 TableView 并导航到您的 Controller View 。
关于ios - 为什么不能在 didSelectRowAtIndexPath 方法中关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853217/