ios - Swift 搜索导航项未更新

标签 ios swift

我创建了一个搜索导航栏,让用户可以轻松地在长列表中选择他们想要的条目。假设用户输入“a”,它只会显示以“a”开头的用户条目。但是,当我在搜索栏上键入任何内容时,什么也没有出现。我在 viewDidLoad 中输入了这段代码。

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    navigationItem.searchController = UISearchController(searchResultsController: nil)
    navigationItem.hidesSearchBarWhenScrolling = false

}

下面是我的典型表格 View 函数。

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    identifiers = ["Adana","Ankara","Antalya","Balikesir","Bursa","Denizli","Diyarbakir","Eskisehir","Gaziantep","Hatay","Icel","Istanbul","Izmir","Kayseri","Kocaeli","Konya","Manisa","Sakarya","Samsun","Sivas","Trabzon","Sanliurfa"]
    //let cell = tableView.dequeueReusableCell(withIdentifier: "CellTwo")
    //cell?.textLabel!.text = identifiers[indexPath.row]
    //return cell!
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "CellTwo")
    cell.textLabel!.text = identifiers[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){


    // here you can use someThing like an array of your segue identifiers
    selectedParamTwo = tableView.cellForRow(at: indexPath)!.textLabel!.text!
    self.performSegue(withIdentifier: "seg2", sender: self)

}

此外,我似乎找不到更改搜索栏中占位符的方法。我将不胜感激任何帮助。提前致谢!

最佳答案

试试这个代码。您需要添加 UISearchBar 委托(delegate)才能使用它。

您需要更正以下内容。

1. 您在 numberOfRowsInSection 中返回静态的 22 行,这是不正确的。首先,您需要确定用户是否搜索任何内容,并根据此返回数据。

2.cellForRowAt 中定义identifiers 数组。与 numberOfRowsInSection 相同,您需要通过检查用户是否搜索任何内容来返回数据。

你需要像下面那样做

首先全局定义 2 个数组第一个是你的标识符数组 第二个是你的搜索器数据数组

var identifiers = [String]()
var arrSearcherData = [String]()
var isSearching: Bool = false    // Add this to identify wether user is searching data or not

viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    identifiers = ["Adana","Ankara","Antalya","Balikesir","Bursa","Denizli","Diyarbakir","Eskisehir","Gaziantep","Hatay","Icel","Istanbul","Izmir","Kayseri","Kocaeli","Konya","Manisa","Sakarya","Samsun","Sivas","Trabzon","Sanliurfa"]

    navigationItem.searchController = UISearchController(searchResultsController: nil)
    navigationItem.searchController?.searchBar.delegate             = self
    navigationItem.searchController?.searchBar.showsCancelButton    = true
    navigationItem.hidesSearchBarWhenScrolling                      = false
    self.definesPresentationContext                                 = true
}

您的 TableView 委托(delegate)和数据源

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isSearching {
            return arrSearcherData.count
        }
        return identifiers.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")

        if isSearching {
            cell.textLabel?.text = arrSearcherData[indexPath.row]
        }
        else {
            cell.textLabel?.text = identifiers[indexPath.row]
        }

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

    }
}

您的 SearcherBar 委托(delegate)

//MARK:- UISearchBar Delegate
extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        self.tblvw.reloadData()

        if searchText.count == 0 {
            isSearching = false
        }
        else {
            isSearching = true
            arrSearcherData = identifiers.filter { $0.contains(searchText) }
        }
        self.tblvw.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = false
        searchBar.text = ""
        searchBar.resignFirstResponder()
        isSearching = false
        self.tblvw.reloadData()
    }
}

关于ios - Swift 搜索导航项未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52215375/

相关文章:

ios - 如何在与默认目标中的数据不同的数据/应用程序(SQLite db、UserDefaults)上运行 Xcode 测试?

ios - 已执行最大重试次数。上传ipa问题

ios - 对 iOS 静态库进行单元测试;访问地址簿

iOS 画中画如果延迟加载则无法启动

ios - countDownTimer = 0 的 UIDatePicker 显示 "0 hours 1 min"

objective-c - Objective C,iOS,有哪些选项可以在 UIImageView 中的图像顶部添加一些文本?

iOS:如何在使用 UIViewContentModeScaleAspectFill 时对齐(左/右)UIImageView?

ios - 试图从 Objective-C 转向 Swift VC

ios - 限制将标签拖出其 superView ios swift

ios - 如何在swift中重载赋值运算符