Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

ios - UISearchController causes black screen Swift 2.0

I have a strange problem in iOS 9 with Swift 2.0. I added UISearchController in my tableViewController but it causes a strange black screen problem. When I press the search bar and write something it shows my filtered results without any problem but when I tap another tab bar button like Bookmarks and after that when I tap tableViewController which is Most Viewed again it shows black screen like screen shot.

There is my tableViewController;

import UIKit

class CitiesTableViewController: UITableViewController, UISearchResultsUpdating {

// MARK: - Class Properties

private var cities = [String]()
private var veterinaries = [Veterinary]()
private var filteredVeterinaries = [Veterinary]()
private var resultSearchController: UISearchController!

// MARK: - TableViewController Life Cycle Methods

override func viewDidLoad() {
    super.viewDidLoad()
    self.getCitiesList()
    self.configureResultsSearchController()
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    self.resultSearchController.active = false
}

// MARK: - Configuring Search Bar Controller

private func configureResultsSearchController() {
    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.hidesNavigationBarDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()
    self.resultSearchController.searchBar.placeholder = "Klinik veya il?e ad?"
    self.tableView.tableHeaderView = self.resultSearchController.searchBar
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.resultSearchController.active { return self.filteredVeterinaries.count }
    else { return self.cities.count }
}

// MARK: - Table view Delegate Methods

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (self.resultSearchController.active) {
        self.performSegueWithIdentifier(Constants.ShowDetailViewControllerSegueIdentifier, sender: nil)
    } else {
        self.performSegueWithIdentifier(Constants.ShowTownsTableViewControllerSegueIdentifier, sender: nil)
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(Constants.CellIdentifier, forIndexPath: indexPath)
    if (self.resultSearchController.active) {
        cell.textLabel?.text = self.filteredVeterinaries[indexPath.row].name
        cell.detailTextLabel?.text = self.filteredVeterinaries[indexPath.row].address
        return cell
    } else {
        cell.textLabel?.text = self.cities[indexPath.row]
        return cell
    }
}

// MARK: - PARSE Query Methods

private func getCitiesList() {
    let parseQueries = ParseQueries()
    parseQueries.downloadListData() {
        (let parseResults) in
        if let veterinaries = parseResults as? [Veterinary] {
            self.veterinaries = veterinaries
            for vet in veterinaries {
                if let city = vet.city {
                    self.cities.append(city)
                }
            }
            dispatch_async(dispatch_get_main_queue()) {
                self.cities = HelperMethods().removeDuplicatesAndSort(array: self.cities)
                self.tableView.reloadData()
            }
        }
    }
}

// MARK: - UISearchController Delegate Methods

func updateSearchResultsForSearchController(searchController: UISearchController) {
    self.filteredVeterinaries.removeAll(keepCapacity: false)
    if let searchBarText = searchController.searchBar.text{
        let searchText = searchBarText.lowercaseString
        // Searching with Veterinary Name and Veterinary City
        self.filteredVeterinaries = self.veterinaries.filter({$0.name?.lowercaseString.rangeOfString(searchText) != nil})
        self.filteredVeterinaries += self.veterinaries.filter({$0.town?.lowercaseString.rangeOfString(searchText) != nil})
        tableView.reloadData()
    }
}

This is the black screen image from iOS 9 simulator same as real device. enter image description here

I think its deiniting my tableView when I tap the searchBar and it can't init again. Is this a bug or something ?

How can I solve this problem ?

Thank you !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Friend, in your viewDidLoad() insert this code line:

self.definesPresentationContext = true

See how I put (line 29):

click here to see


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...