UISearchController保留问题 [英] UISearchController retain issue

查看:231
本文介绍了UISearchController保留问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use UISearchController however I confronted with retain issue that I can't solve. MainTableview has two sections.

第1节

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

Filtered Data based on some Regex

第2节

所有数据

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

I added UISearchController to my tableview and attached ResultsTableController as resultsTableController. It works when user search something, ResultsTableController comes forward and because I set tableview delegate to self, selecting item from ResultsTableController calls didSelectRowAtIndexPath in my MainTableViewController. However I have allocation issue if user selects something from resultsTableController.

以下情况会发生在不同的情况下

Following happens for different scenarios

  • 用户不搜索任何内容,只是从中选择一个项目 MainTableview,我看到了deinit消息
  • 用户搜索某些内容,取消搜索,然后从中选择项目 MainTableview,我看到了deinit消息
  • 用户搜索某些内容,然后从中选择一项 ResultsTableController,我在视图控制器中没有得到deinit
  • User doesn't search anything, just selects an item from MainTableview, I see deinit messages
  • User searches something, cancel the search, select item from MainTableview, I see deinit messages
  • User searches something, and selects an item from ResultsTableController, I don't get deinit in my viewcontrollers

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来解决保留问题

Hey there I ran into the issue today apparently I need to force the dismiss of the searchController to work around the retain issue

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

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

这篇关于UISearchController保留问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆