首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchController in a UIViewController

UISearchController in a UIViewController
EN

Stack Overflow用户
提问于 2014-10-17 03:51:44
回答 4查看 18.1K关注 0票数 14

我希望在Swift中创建类似于苹果地图应用程序的功能。是否存在将UISearchController集成到常规视图中(即:不是UITableView)。在连接的搜索栏中单击后,通过Storyboard将其中一个放入其中会导致崩溃。或者有什么方法可以通过UITableView实现这一结果呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-10-17 06:09:05

我在情节提要中的视图控制器中添加了搜索栏和搜索显示控制器。视图控制器只包含搜索栏和搜索显示控制器,并且没有自己的TableView。当您在视图控制器中添加搜索栏时,它会自动将视图控制器设置为其委托。

现在,Search和有了自己的表视图,当您单击框内并开始键入时,它将用于显示搜索结果。此表视图期望您的视图控制器为其提供numberOfRowsInSection和cellForRowAtIndexPath函数的实现,以正确显示数据。

当您在没有这些内容的情况下运行您的项目并在搜索栏内点击时,您将得到以下错误:

tableView:numberOfRowsInSection:]:未识别的选择器发送给实例0x7fbf63449660 *由于未识别的异常'NSInvalidArgumentException‘而终止应用程序

如果您看到,错误发生在numberOfRowsInSection方法上。

将视图控制器定义更改为

代码语言:javascript
复制
class ViewController: UIViewController

代码语言:javascript
复制
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource

并落实所需的方法,包括:

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return UITableViewCell()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0
}

我刚刚在上面的方法中添加了默认返回值。

现在,如果您在搜索视图委托方法中筛选出数据源,并正确设置上述两种方法中的行数和单元格信息,那么它应该可以工作。

希望这能有所帮助!

票数 3
EN

Stack Overflow用户

发布于 2014-11-05 13:13:44

如果您想将UISearchController与非UITableView一起使用,下面是我是如何做到的。

因为UISearchController还没有(现在!)在IB支持下,您可以执行而不是需要在其中添加任何内容,比如UISearchBar

代码语言:javascript
复制
@interface UIViewControllerSubclass () <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>

@property (strong, nonatomic) UISearchController *searchController;

@end

@implementation UIViewControllerSubclass

- (void)viewDidLoad
{        
    [super viewDidLoad];
    // Do any custom init from here...

    // Create a UITableViewController to present search results since the actual view controller is not a subclass of UITableViewController in this case
    UITableViewController *searchResultsController = [[UITableViewController alloc] init];

    // Init UISearchController with the search results controller
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

    // Link the search controller
    self.searchController.searchResultsUpdater = self;

    // This is obviously needed because the search bar will be contained in the navigation bar
    self.searchController.hidesNavigationBarDuringPresentation = NO;

    // Required (?) to set place a search bar in a navigation bar
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

    // This is where you set the search bar in the navigation bar, instead of using table view's header ...
    self.navigationItem.titleView = self.searchController.searchBar;

    // To ensure search results controller is presented in the current view controller
    self.definesPresentationContext = YES;

    // Setting delegates and other stuff
    searchResultsController.tableView.dataSource = self;
    searchResultsController.tableView.delegate = self;
    self.searchController.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;        
}

@end

我希望这足以发挥作用:)

当然,至少需要实现UITableViewDelegate、UITableViewDataSource、UISearchResultsUpdater方法。

享受吧!

票数 38
EN

Stack Overflow用户

发布于 2015-08-28 16:18:14

我想自己找出UISearchController。将其设置为titleView是很方便的,但是在我的页面上,我不得不将searchBar放在UIViewController的顶部。

代码语言:javascript
复制
// Add a normal View into the Storyboard.
// Set constraints:
// - height: 44
// - leading and trailing so that it spans the width of the page
// - vertical position can be anywhere based on your requirements, like near the top
@IBOutlet weak var searchContainerView: UIView!

var searchResultsController = UISearchController()

override func viewDidLoad() {
    // TODO: set the searchResultsController to something
    let controller = UISearchController(searchResultsController: nil)
    // have the search bar span the width of the screen
    controller.searchBar.sizeToFit()
    // add search bar to empty View
    searchContainerView.addSubview(controller.searchBar)
    searchResultsController = controller
}

更新:

在一两个项目中实现了UISearchController之后,我发现自己被@adauguet将搜索栏嵌入导航栏的方法吸引住了。

这是Swift的代码。不过,一个不同之处在于,它没有设置searchBar委托,因为searchResultsUpdater已经侦听了文本更改。

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
//        locationManager.delegate = self
//        locationManager.desiredAccuracy = kCLLocationAccuracyBest
//        locationManager.requestWhenInUseAuthorization()
//        locationManager.requestLocation()
    let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable
    resultSearchController = UISearchController(searchResultsController: locationSearchTable)
    resultSearchController?.searchResultsUpdater = locationSearchTable
    let searchBar = resultSearchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Search for places"
    navigationItem.titleView = resultSearchController?.searchBar
    resultSearchController?.hidesNavigationBarDuringPresentation = false
    resultSearchController?.dimsBackgroundDuringPresentation = true
    definesPresentationContext = true
}

此外,我还写了一篇博客文章,从零开始创建一个使用UISearchController显示地图搜索结果的项目。它还可以完成地图项目中您可能需要的其他事情,比如获取用户位置、放下引脚、将placemarks解析为单行地址,以及创建带您到Apple Maps的标注按钮以获取驾驶方向。

http://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/

博客文章很长,所以如果您只想跳过代码,下面是相关的git:

https://github.com/ThornTechPublic/MapKitTutorial

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26417591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档