ios - 在 UISearchController Swift 中将标题与图像名称匹配

标签 ios swift uitableview dictionary uisearchbar

我有一个 tableviewController,其中有一个 SearchBar。当我在搜索框中输入自助餐厅名称时,会显示错误的图像和找到的名称。

这是我的变量:我应该用另一种语法格式声明带有标题和图像的字典数组吗?

@IBOutlet weak var RetailTableView: UITableView!  //our TableView
        var resultSearch = UISearchController() //our UISearchbar

        var menuItems = [["20/20 Cafe" : "20_20Cafe.jpg"],
                        ["Au Bon Pain": "AuBonPain.jpg"]]

        var menuItemsFiltered = [(String)]()

这是我的 TableViewFunc:

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {  
        let cell = tableView.dequeueReusableCellWithIdentifier("RetailCell", forIndexPath: indexPath) as! ResidentialTableViewCell

        let entry = menuItems[indexPath.row]
        let image = UIImage(named: RetailImagesArray[indexPath.row])

                if self.resultSearch.active
                {
                    cell.RetailLabel.text = menuItemsFiltered[indexPath.row]
                }
                else
                {
                    cell.RetailImage.image = image
                    cell.RetailLabel.text = entry.keys //Not Working
                }

我的搜索功能也不行

 func updateSearchResultsForSearchController(searchController: UISearchController) {

        self.FilteredArray.removeAll(keepCapacity: false)
        let searchText = searchController.searchBar.text!.lowercaseString
        FilteredArray = RetailPlacesArray.filter({$0.lowercaseString.rangeOfString(searchText) != nil})

        RetailTableView.reloadData()
    }

请帮忙,我做错了什么?

最佳答案

如果未找到字符串,

rangeOfString 不会返回 nil,而是返回位置为 NSNotFoundNSRange .

关于ios - 在 UISearchController Swift 中将标题与图像名称匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285121/

相关文章:

ios - NSCalendar currentCalendar 在 cocoapod 内部调用时返回 nil?

swift - 使用 KIF 的简单测试用例失败

没有年份的 iOS 日期格式,具有正确的本地化

swift - 如何以编程方式将 UILabel 和 UiImageView 添加到 TableView 单元格内的 UIView?

ios - 使用 `ipa build` 和 `deliver` 工具后出现无效的临时配置文件错误(自动提交应用程序)

iphone - 单个屏幕中的多个 TableView

ios - Contact Framework 手机数组

swift - 将框架构建为 "Generic iOS Device"导致在使用框架的项目中使用未声明的类型

ios - 将图像加载到 UITableView 中的自定义单元格时发生内存泄漏

ios - TableView 上的搜索栏不起作用