标签: uisearchbar

searchBarSearchButtonClicked 不起作用 Swift

我正在尝试使用 UISearchBar 但它对我不起作用

这是我的代码

class searchViewController: UIViewController  , UITableViewDataSource , UISearchBarDelegate{
    var searchResults = [String]()
    @IBOutlet weak var searchTable: UITableView!
    @IBOutlet weak var mySearchBar: UISearchBar!
    override func viewDidLoad() {
        super.viewDidLoad()


    }



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = searchTable.dequeueReusableCellWithIdentifier("searchCell", forIndexPath: indexPath) as! UITableViewCell


        return cell
    }
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        print("print anything ")
    }

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        print(self.mySearchBar.text)
    }



}
Run Code Online (Sandbox Code Playgroud)

当我使用模拟器单击键盘中的搜索按钮时,没有发生任何事情

我试过 …

uisearchbar swift

0
推荐指数
1
解决办法
4870
查看次数

如何使用 UISearchBar 过滤 NSDictionary?

我需要通过过滤数组来更改我的搜索功能:

func filterContentForSearchText(searchText: String, scope: String = "All") {
    if searchText != "" {

        filteredName = CGCoins.shared.ids.filter {name in
            return   name.lowercased().contains(searchText.lowercased())}
    } else { filteredName = CGCoins.shared.ids

    }
}
Run Code Online (Sandbox Code Playgroud)

过滤包含符号及其相应名称的字典。理想情况下,我希望能够同时搜索键和值,例如,字典 (CGCoins.shared.coinDictionary) 如下所示:

["btc":"bitcoin","eth":"ethereum","ltc":"litecoin"...]
Run Code Online (Sandbox Code Playgroud)

所以我希望能够使用 UISearchBar 进行搜索,并且能够搜索“btc”并返回“bitcoin”或搜索“bitcoin”并返回“bitcoin”。

我试过这个:

func filterNewForSearchText(searchText: String, scope: String = "All") {
    if searchText != "" {

        filteredName = CGCoins.shared.coinDictionary.filter {name in
            return   name.key == searchText}
    } else { filteredName = CGCoins.shared.coinDictionary.values

    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

无法将“[String : String]”类型的值分配给“[String]”类型

我怎样才能成功地为键和值过滤字典,并返回搜索到的任何内容的值?也欢迎任何替代解决方案。

xcode nsdictionary uisearchbar ios swift

0
推荐指数
1
解决办法
391
查看次数

(Swift) 即使没有文本,如何在搜索栏上显示取消按钮?

我有一个带Cancel按钮的搜索栏。取消按钮仅在我开始在搜索栏中键入文本时出现。
我想要的是在用户点击搜索栏的那一刻显示取消按钮(当光标和键盘出现时)。

代码:

let searchController = UISearchController(searchResultsController: nil)
    var isSearchBarEmpty: Bool {
        return searchController.searchBar.text?.isEmpty ?? true
    }
    var searching = false
Run Code Online (Sandbox Code Playgroud)

搜索栏方法:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        let cancelButtonAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemGreen]
        UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)

        searching = true
        searchBar.showsCancelButton = true
        tableView.reloadData()
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
    {
        searching = false
        self.searchBar.endEditing(true)
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searching = false
        searchBar.text = nil
        searchBar.showsCancelButton = false
        searchBar.endEditing(true)
        tableView.reloadData()
    }
Run Code Online (Sandbox Code Playgroud)

uisearchbar ios swift

0
推荐指数
1
解决办法
632
查看次数

在 Swift 中,如何在由 struct 对象数组组成的字典中进行搜索?

我有一个结构:

struct AlimentObject {
        var id = UUID().uuidString
        var nomAliment = "Nouvel aliment"
        var poids : Float = 100
        var calories : Float = 0
}
Run Code Online (Sandbox Code Playgroud)

以及由 AlimentObjet 组成的数组:

   var filteredData: [AlimentObject]!
Run Code Online (Sandbox Code Playgroud)

我有一个表格视图的搜索栏,但我不知道如何搜索“nomAliment”中的字符,这里是搜索栏的代码:

extension AjoutAlimentController: UISearchBarDelegate {
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filteredData = []
        for aliment in data {
            if aliment.nomAliment.lowercased().contains(searchText.lowercased) {
                filteredData.append(aliment)
            }
        }
        self.tableView.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

错误说:类型 '() -> String' 不能符合 'StringProtocol'; 只有结构/枚举/类类型可以符合它在行中的协议:

if aliment.nomAliment.lowercased().contains(searchText.lowercased) {
Run Code Online (Sandbox Code Playgroud)

filter uitableview uisearchbar swift

0
推荐指数
1
解决办法
41
查看次数

使用iOS 9上的搜索栏

我有一个cities数组,我希望用户添加.

add screen我想,当用户开始打字,为城市的选项(从阵列)将显示在一个表视图.

我在互联网上搜索了几个小时,但一切似乎都很旧而且不是最新的(苹果在iOS 8和9中弃用了一些东西).

我正在寻找有关如何做到这一点的最新教程,任何人都可以提供帮助吗?

谢谢!

iphone cocoa-touch objective-c uisearchbar ios

-1
推荐指数
1
解决办法
6017
查看次数

SWIFT UISearchBar 文本从右到左不正确对齐

我试图以编程方式设置从左到右对齐的搜索栏文本,但似乎它在相反的方向(如英语)添加了文本,然后搜索没有结果。感谢任何人的帮助。

uisearchbar uisearchbardelegate swift swift3 swift4

-1
推荐指数
1
解决办法
1407
查看次数