标签: uisearchcontroller

如何在UISearchController中使用范围栏

我能够为UISearchController创建范围栏并设置它们的标题

    resultSearchController = ({

        let controller = UISearchController(searchResultsController: nil)

        controller.searchResultsUpdater = self

        controller.dimsBackgroundDuringPresentation = false

        controller.searchBar.showsScopeBar = true

        controller.searchBar.scopeButtonTitles = ["One", "two", "three"]

        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar


        return controller

    })()
Run Code Online (Sandbox Code Playgroud)

但是我如何使用范围栏进行实际排序.我目前的排序方法如下

func updateSearchResultsForSearchController(searchController: UISearchController) {

    filteredTableData.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)

    let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)

    filteredTableData = array as! [String]

    tableView.reloadData()

}
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift uisearchcontroller

7
推荐指数
1
解决办法
3896
查看次数

UISearchController与UIBarPositionTopAttached将UISearchBar抛出屏幕; 不可能有UITableView的标准UISearchController?

我正在尝试为iOS 8中的Contacts应用程序创建类似的体验.其主要组成部分是:

  1. 将搜索栏固定在导航栏下方
  2. 在呈现时,将搜索栏附加到视图的顶部(标准功能).

然而,这说起来容易做起来难.经过一段时间的挣扎tableHeaderView(不允许在桌面视图前进行交互,并且viewDidLayoutSubviews定位很复杂),我决定嵌入一个UITableView内部UIViewController,所以我可以添加UISearchBar为子视图.这非常有效,并允许在所有滚动位置与搜索栏进行交互,并且插图不难计算.

但是,激活后,搜索栏会在状态栏下方被切断.看起来像一个简单的问题 - 即使我没有在一个完全相同的实现中体验它UITableViewController.因此,我尝试确保在每个可能的视图控制器中设置所有属性以进行对齐.

self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
Run Code Online (Sandbox Code Playgroud)

我在viewWillAppear和viewDidLoad以及半透明导航栏和表视图和搜索栏的不同起始帧中尝试过各种可能的组合.没运气.所以,我尝试调整帧或约束,可能使用topLayoutGuide或只是0.不幸的是,在任何UISearchControllerDelegate方法中调整帧实际上并没有调整其呈现位置,并且当活动动画开始时添加约束立即崩溃(由于super.top当时不存在于视图层次结构中;删除willPresent中的约束绝对没有任何结果.

经过一段时间的挣扎后,我尝试实施positionForBar作为UISearchBar代表:

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}
Run Code Online (Sandbox Code Playgroud)

这似乎调整了高度,但搜索栏飞离了视图的顶部.激活后,搜索栏似乎直接出现在可见区域上方.这比在它后面或下面被剪裁更糟糕UIStatusBar.我还尝试在搜索控制器变为活动状态时隐藏状态栏,但有条件实现prefersStatusBarHidden根本不起作用(返回YES工作很好而没有UISearchController激活,但是当它处于活动时它再次显示状态栏或它显示在它下面).我认为这是因为UISearchController拒绝服从任何标准或规则,现在很痛苦.

调试层次结构非活动状态活跃的国家

我一直试图解决这个问题几天,除了UISearchController完全重新实现类/动画之外,我想不出解决方案.请帮忙!

objective-c uitableview uiviewcontroller ios uisearchcontroller

7
推荐指数
1
解决办法
1457
查看次数

范围按钮更改时如何更新搜索结果.Swift UISearchController

如何更改范围按钮(在点击范围后)更新搜索结果?当我再次输入时,搜索结果已更改(使用新范围)!

searchControl - config import UIKit

class ProductTableView: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating
{

    @IBOutlet weak var tableView: UITableView!
    var searchController: UISearchController!

    var friendsArray = [FriendItem]()
    var filteredFriends = [FriendItem]()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        searchController.searchBar.sizeToFit()
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.scopeButtonTitles = ["Title","SubTitle"]
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar

        self.tableView.reloadData()


    }
Run Code Online (Sandbox Code Playgroud)

更新功能当我输入文本时,NSLog会打印我的文本和范围编号.当我改变范围 - 什么都没有!

func updateSearchResultsForSearchController(searchController: UISearchController) {
    let searchText = searchController.searchBar.text
    let scope = searchController.searchBar.selectedScopeButtonIndex
    NSLog("searchText - \(searchText)")
    NSLog("scope - \(scope)")
    filterContents(searchText, scope: scope) …
Run Code Online (Sandbox Code Playgroud)

scope swift uisearchcontroller

7
推荐指数
1
解决办法
4290
查看次数

updateSearchResults()没有被调用

我已经在SO上阅读了类似的问题和解决方案.但似乎没有一个能解决我的问题.我正在使用自定义搜索控制器和自定义搜索栏,并且没有调用func updateSearchResults(对于searchController:UISearchController).

    var customSearchController: CustomSearchViewController!
Run Code Online (Sandbox Code Playgroud)

CustomSearchViewController:在ViewDidLoad()中

customSearchController = CustomSearchViewController(searchResultsController: ***nil***, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: searchTableView.frame.size.width, height: 44.0), searchBarFont: UIFont(name: "HelveticaNeue", size: 16.0)!, searchBarTextColor: UIColor.purple, searchBarTintColor: UIColor.white)

customSearchController.searchResultsUpdater  = self
customSearchController.definesPresentationContext = true
customSearchController.customSearchBar.placeholder = "What are you looking for?"
customSearchController.customSearchBar.backgroundColor = UIColor.white
customSearchController.customSearchBar.sizeToFit()
customSearchController.customSearchBar.resignFirstResponder()
customSearchController.customSearchBar.showsCancelButton = true
customSearchController.customSearchBar.delegate = self
Run Code Online (Sandbox Code Playgroud)

没有被召唤::(

func updateSearchResults(for searchController: UISearchController) {
    filtered.removeAll()
    filtered = searchArray.filter({ (text) -> Bool in
        let tmp: NSString = text as NSString
        let range = tmp.range(of: customSearchController.customSearchBar.text!, …
Run Code Online (Sandbox Code Playgroud)

search searchbar uisearchcontroller

7
推荐指数
2
解决办法
1309
查看次数

6
推荐指数
3
解决办法
7942
查看次数

UISearchController如何工作?

它怎么UISearchController做呢?

  • 它如何将其转换UISearchBar为类似导航栏的视图?
  • 它如何将其UISearchBar放入另一个视图层次结构并在以后恢复?
  • 它是如何使用UIViewController表示API 完成所有这些的?
  • 即使在所有呈现的视图控制器全屏的iPhone上,它如何仅覆盖呈现视图控制器的视图?

我可以在UISearchController不使用私有API 的情况下自行创建吗?

cocoa-touch uikit ios ios8 uisearchcontroller

6
推荐指数
1
解决办法
171
查看次数

带有SearchController的TableView - 未调用DEINIT

我在界面构建器中添加了一个搜索栏和搜索显示控制器到我的应用程序.我无法正确地进行deinit(dealloc).

它显示以下行为(swift2,ios9):

  • 用户不搜索任何内容,只需从tableView中选择一个项目,调用DEINIT即可
  • 用户搜索某些内容(或只搜索搜索栏中的内容),取消搜索,从tableView中选择项目,调用DEINIT
  • 用户搜索某些东西(或只是点击搜索栏),然后从tableView中选择一个项目,不调用DEINIT :(

如果我在导航控制器中选择"返回"而不是选择项目,则会出现相同的行为.

code removed - refer to COMPLETE CODE at bottom of post.
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏!

更新进一步测试表明,从视图控制器中删除progressHud/loadingHud完全没有影响DEINIT没有被调用.它必须与tableview或searchcontroller本身有关...

更新2我尝试在viewWillDissapear中调用searchBarCancelButtonClicked()方法,但仍然没有释放.即使你点击"取消"然后导航它就会......

更新3将willDisappear/didDisappear更改为以下对DEINIT没有影响 - 但不会给出错误的界面问题(感谢Polina).我试图找出我能发布的任何东西,但到目前为止还没有运气.

    override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)
    searchBarCancelButtonClicked(searchController.searchBar)
}

override func viewDidDisappear(animated: Bool) {
    print("View did disappear")
    searchController.searchBar.resignFirstResponder()
    searchController.searchBar.endEditing(true)
    searchController.active = false
    loadingHud.removeFromSuperview()
    progressHud.removeFromSuperview()
    searchController.searchBar.delegate = nil
    searchController.searchResultsUpdater = nil
    searchController = nil
    tableView = nil

    super.viewDidDisappear(true)

}
Run Code Online (Sandbox Code Playgroud)

更新4我找不到答案.真的希望有人可以帮忙!

更新5响应@ConfusedByCode - 我更新了以下方法,以便[unowned self] in在所有闭包或后台线程操作中使用:

code removed - …
Run Code Online (Sandbox Code Playgroud)

uitableview retain ios swift uisearchcontroller

6
推荐指数
2
解决办法
2023
查看次数

如何搜索键盘中的搜索按钮?

我的tableview标题中有一个搜索栏.为此,我正在使用uisearchcontroler.但是当我在搜索栏中发短信时它会更新tablview数据,我需要在点击键盘上的搜索按钮时更新tablview,因为我在api中获取搜索数据,每次当我在搜索栏中发短信时它都会请求并且需要很长时间.我怎么解决这个问题?

var resultSearchController = UISearchController()
var indicator:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
var ref=UIRefreshControl()
override func viewDidLoad() {
    super.viewDidLoad()


    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    indicator.center = view.center
    view.addSubview(indicator)
    indicator.bringSubviewToFront(view)

    indicator.startAnimating()
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    getRequests.getType1(tableView,spinner: indicator)

    ref.addTarget(self, action: #selector(Catalog1TableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
    ref.attributedTitle = NSAttributedString(string: "????????")
    tableView.addSubview(ref)

}


func updateSearchResultsForSearchController(searchController: UISearchController)
{
    indicator.startAnimating()
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    tableView.reloadData()
    getRequests.getProduct(tableView, spinner: indicator,name: searchController.searchBar.text!)
    for …
Run Code Online (Sandbox Code Playgroud)

ios swift uisearchcontroller

6
推荐指数
1
解决办法
3312
查看次数

在iOS11中的tableHeaderView中使用UISearchController和UISearchBar

有没有人成功地保留了UISearchBartableHeaderView一个UITableView在ios11?iPhone X在景观中通常会出现问题:

在此输入图像描述

Apple建议使用新searchController属性:

if (@available(iOS 11.0, *)) {
    self.navigationItem.searchController = self.searchController;
    self.navigationItem.hidesSearchBarWhenScrolling = NO;
} else {
    self.tableView.tableHeaderView = self.searchController.searchBar;
}
Run Code Online (Sandbox Code Playgroud)

然而,这并不总是有效.这是一个代码示例:

嵌入UISplitViewController时,UINavigationBar中缺少iOS11 UISearchBar

所以,试图保持searchBartableHeaderView-切实可行的第一步是应用适当的自动布局限制:

if (@available(iOS 11.0, *)) {
    UILayoutGuide *guide = self.tableView.safeAreaLayoutGuide;
    searchBar.translatesAutoresizingMaskIntoConstraints = NO;
    [searchBar.leftAnchor constraintEqualToAnchor:guide.leftAnchor].active = YES;
    [searchBar.rightAnchor constraintEqualToAnchor:guide.rightAnchor].active = YES;
}
Run Code Online (Sandbox Code Playgroud)

searchBar显示时,那么正确的尺寸:

在此输入图像描述

但是,UISearchBar激活时会出现问题:

在此输入图像描述

这似乎是一个老问题.有许多stackoverflow问题和答案与这样的问题有关.在这种情况下,在检查视图层次结构时,似乎设置的宽度UISearchBar不正确.所以这可以纠正:

- (void)didPresentSearchController:(UISearchController *)searchController NS_AVAILABLE_IOS(11_0)
{
    if (@available(iOS 11.0, *)) {
        CGRect frame = …
Run Code Online (Sandbox Code Playgroud)

uitableview uisearchbar ios uisearchcontroller ios11

6
推荐指数
0
解决办法
1729
查看次数

Swift - 覆盖tableview函数 - Method不会覆盖其超类中的任何方法

我想实现UISearchController,使用本教程一步一步:

使用MKLocalSearchRequest搜索地点并使用UISearchController显示结果

在本节中......

使用MKLocalSearchRequest搜索位置

...我在步骤3中遇到问题.设置表视图数据源

extension LocationSearchTable {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return matchingItems.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = ""
    return cell
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:方法不会覆盖其超类中的任何方法

我正在尝试overridecellForRowAtindexPathtableView函数中删除,在这种情况下我在Xcode中没有错误,但执行后出错:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'UITableView (<UITableView: 0x7ff8e03a3800; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H;
gestureRecognizers = <NSArray: …
Run Code Online (Sandbox Code Playgroud)

mapkit ios mklocalsearchrequest swift uisearchcontroller

6
推荐指数
1
解决办法
3940
查看次数