ios - UISearchController 保留问题

标签 ios swift uitableview uisearchcontroller

我正在尝试使用 UISearchController,但是我遇到了无法解决的保留问题。 MainTableview 有两个部分。

第 1 节

基于一些正则表达式过滤数据

第 2 节

所有数据

我将 UISearchController 添加到我的 tableview 并将 ResultsTableController 作为 resultsTableController 附加。当用户搜索某些内容时,它会起作用,ResultsTableController 会出现,因为我将 tableview 委托(delegate)设置为 self ,从 ResultsTableController 中选择项目会在我的 MainTableViewController 中调用 didSelectRowAtIndexPath。但是,如果用户从 resultsTableController 中选择某些内容,我会遇到分配问题。

针对不同的场景会发生以下情况

  • 用户不搜索任何内容,只是从中选择一个项目 MainTableview,我看到 deinit 消息
  • 用户搜索内容,取消搜索,从中选择项目 MainTableview,我看到 deinit 消息
  • 用户搜索内容,并从中选择一个项目 ResultsTableController,我没有在我的 View Controller 中取消初始化

MainTableViewController.swift

var searchController: UISearchController!

// Secondary search results table view.
var resultsTableController: ResultsTableController!
var allCompanies = ["Data1","Data2","Data3"]

override func viewDidLoad() {
    super.viewDidLoad()
     resultsTableController = ResultsTableController()
    // We want to be the delegate for our filtered table so didSelectRowAtIndexPath(_:) is called for both tables.
    resultsTableController.tableView.delegate = self
    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.sizeToFit()
    tableView.tableHeaderView = searchController.searchBar

    searchController.delegate = self
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.searchBar.delegate = self   
    definesPresentationContext = true
    }
}



// MARK: UISearchBarDelegate

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}

// MARK: UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController) {
    // Update the filtered array based on the search text.

    let filteredResults = allCompanies.filter({ company in
        (company.lowercaseString as NSString).containsString(searchController.searchBar.text.lowercaseString)
    })

    // Hand over the filtered results to our search results table.
    let resultsController = searchController.searchResultsController as! ResultsTableController
    resultsController.searchResult = filteredResults
    resultsController.tableView.reloadData()
}

// usual tableview methods

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if resultsTableController.searchResult.count > 0 {
        selectedCompany = resultsTableController.searchResult[index]
        //do something with selected company
        navigationController?.popViewControllerAnimated(true)
        return
     }
     //
     selectedCompany = allCompanies[index]
      navigationController?.popViewControllerAnimated(true)

}

deinit {
    println("MainTableView deinit")
}

ResultTableController.swift

class ResultsTableController:UITableViewController {

     var searchResult = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
     }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return searchResult.count
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
        let index = indexPath.row
        cell.textLabel?.font = UIFont(name: "Avenir-Roman", size: 16)
        cell.textLabel?.text = searchResult[index].description
        return cell

    }

    deinit {
        println("ResultTableController deinit")
    }
}

最佳答案

嘿,我今天遇到了这个问题 显然我需要强制关闭 searchController 以解决保留问题

  override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    searchController?.dismissViewControllerAnimated(false, completion: nil)
  }

这是我的示例项目 https://www.dropbox.com/s/zzs0m4n9maxd2u5/TestSearch.zip?dl=0

关于ios - UISearchController 保留问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141155/

相关文章:

objective-c - 如何加载带有字典数组的 tableView?

iphone - 无法更改 UITableViewCell 背景颜色

ios - 如何更改 UIAlertAction 字体或在第二个位置显示 UIAlertActionStyle.cancel 样式 ActionButton?

iphone - 杀死应用程序后显示本地通知弹出窗口

swift - 关闭意外要求抛出(Firebase)

ios - 数组中的多个项目给出符合编码的错误

Android:相当于 iOS UIActionSheet

点击按钮时出现 iOS EXC_BAD_ACCESS 错误

ios - 设置 rowHeight 等于 UITableViewAutomaticDimension 不起作用

ios - Swift +隐藏或删除 UIView