Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Selecting Search Bar Results

After I filter the table with the search bar I cannot select the results. If I try to select them it just takes me back to the main tableView. What am I missing here? How come I can only select results of this tableView when the search bar is not engaged?

import UIKit
import Foundation

class BenchmarkWODViewController: UITableViewController, UISearchResultsUpdating {

    var WodList = [WOD]()
    var FilteredWodList = [WOD]()
    var Keyword = ""
    var searchController : UISearchController?
    var index = Int()

    @IBAction func backButton(sender: AnyObject) {
    self.navigationController?.popViewControllerAnimated(true)
    }




    override func viewDidLoad() {
        super.viewDidLoad()

        for wodData in BenchmarkWODs.library {
            let wod = WOD(dictionary: wodData)
            WodList.append(wod)
        }

        // Search Bar
        self.searchController = UISearchController(searchResultsController: nil)
        self.searchController?.searchBar.autocapitalizationType = .None
        self.tableView.tableHeaderView = self.searchController?.searchBar
        self.searchController?.searchResultsUpdater = self
        self.Keyword = ""
        definesPresentationContext = true

        self.filterByName()
    }


    func filterByName(){
        self.FilteredWodList = self.WodList.filter({ (wod: WOD) -> Bool in
            if self.Keyword.characters.count == 0 {
                return true
            }

            if (wod.name?.lowercaseString.rangeOfString(self.Keyword.lowercaseString)) != nil {
                return true
            }
            return false
        })
        self.tableView.reloadData()
    }


    // Search Bar Function
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        Keyword = searchController.searchBar.text!
        self.filterByName()

    }



    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.FilteredWodList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("BenchmarkCell", forIndexPath: indexPath) as UITableViewCell

        let wod = self.FilteredWodList[indexPath.row]

        if let wodName = wod.name {
            cell.textLabel?.text = wodName
        }

        return cell
    }

1 Answer

Well I figured it out myself. I don't know why but apparently this works for some reason.

searchController?.dimsBackgroundDuringPresentation = false