首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchController & Segue

UISearchController & Segue
EN

Stack Overflow用户
提问于 2020-06-15 17:24:08
回答 1查看 61关注 0票数 0

我正在学习UISearchController,发现我遇到了以下问题。

当我在搜索栏中键入文本时,

  1. 对记录进行了筛选并正确显示。当我单击该记录时,应该将其保存到另一个ViewController以显示详细视图。但它回到了过滤的结果。

  1. 第二个问题是,在我单击该记录之后,它将显示未经过滤的记录的信息,而不是过滤的记录。

代码语言:javascript
复制
class Method2VC: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var item = [Item]()
    var searchResults = [Item]()
    var searchController: UISearchController!

    var isSearchBarEmpty: Bool {
        return searchController.searchBar.text?.isEmpty ?? true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self

        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self

        // Setup Search Bar information
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = true

        searchController.hidesNavigationBarDuringPresentation = false
        searchController.dimsBackgroundDuringPresentation = true
        searchController.searchBar.placeholder = "输入搜索信息"
        searchController.definesPresentationContext = true

        fetchData()

    }

    func fetchData() {
        let fetchRequest: NSFetchRequest<Item> = Item.fetchRequest()
        // let fetchRequest = NSFetchRequest<Item>(entityName: "Item")
        let modelSort = NSSortDescriptor(key: "model", ascending: true)
        fetchRequest.sortDescriptors = [modelSort]
        do {
            let fetchedResults = try context.fetch(fetchRequest)
            item = fetchedResults

        } catch {
            fatalError("Failed to initialize FetchedResultsController: \(error)")
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationController = segue.destination as! DetailVC
                destinationController.item = !isSearchBarEmpty ? searchResults[indexPath.row] : item[indexPath.row]
            }
        }
    }
}

// MARK: - UITableView Datasource
extension Method2VC: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if !isSearchBarEmpty {
            return searchResults.count
        } else {
            return item.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)

        // Determine the data - Original Data or SearchResults Data
        let model = !isSearchBarEmpty ? searchResults[indexPath.row] : item[indexPath.row]

        // Configure Cell
        cell.textLabel?.text = model.model
        return cell
    }
}

// MARK: - UISearchResultUpdating
extension Method2VC: UISearchResultsUpdating {

    func updateSearchResults(for searchController: UISearchController) {
        if !isSearchBarEmpty {
            if let searchText = searchController.searchBar.text {
                filterContent(for: searchText)
                tableView.reloadData()
            }
        }
    }

    func filterContent(for searchText: String) {
        searchResults = item.filter({ (item) -> Bool in
            if let model = item.model {
                let isMatch = model.localizedStandardContains(searchText)
                return isMatch
            }
            return false
        })
    }
}

请给我建议。

加伦,向你问好

EN

回答 1

Stack Overflow用户

发布于 2020-06-20 05:32:57

将obscuresBackgroundDuringPresentation更改为false。

对于我的情况,我使用dimsBackgroundDuringPresentation。这是不可取的。

由于我使用的是相同的ViewController,因此上述设置必须设置为false。

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

https://stackoverflow.com/questions/62393600

复制
相关文章

相似问题

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