Search code examples
iosswiftsearchdisplaycontroller

Q: Search Bar and Search Display Controller in storyboard with swift and Xcode8.0 crash at Appdelegate


Storyboard layout is below:

This is my storyboard layout

This is my demo, and I don't know where the error comes out.

In ViewController:

import UIKit
import Foundation

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!


@IBOutlet var searchDisplay: UISearchDisplayController!

var ctrls:[String] = ["Label","Button1-init","Button1-high","Button2-init","Button2-high","Switch"]

var ctrlsel:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.ctrlsel = self.ctrls
    self.searchDisplay.searchResultsTableView.register(UITableViewCell.self,
                                                            forCellReuseIdentifier: "SwiftCell")

    self.searchDisplay.searchBar.placeholder = "input"

    //self.searchDisplay.searchBar.text = "b"

    self.searchDisplay.searchBar.prompt = "search"

    self.searchDisplay.searchBar.searchBarStyle = UISearchBarStyle.minimal

    self.searchDisplay.searchBar.showsScopeBar = true
    self.searchDisplay.searchBar.scopeButtonTitles = ["all","part","high"]
}

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


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

    let identify:String = "SwiftCell"

    let cell = self.searchDisplay.searchResultsTableView.dequeueReusableCell(
        withIdentifier: identify, for: indexPath as IndexPath) as UITableViewCell
    cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    cell.textLabel?.text = self.ctrlsel[indexPath.row]
    return cell
}

func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
    self.searchText = searchText
    searchCtrls()
}

func searchBar(searchBar: UISearchBar!, selectedScopeButtonIndexDidChange selectedScope: Int) {
    print(selectedScope)
    searchCtrls();
}

var searchText:String = ""


func searchCtrls() {

    if self.searchText == "" {
        self.ctrlsel = self.ctrls
    }
    else {
        var scope = self.searchDisplay.searchBar.selectedScopeButtonIndex;

        self.ctrlsel = []
        for ctrl in self.ctrls {
            let lc = ctrl.lowercased()
            if lc.hasPrefix(self.searchText) {
                if (scope == 0 || (scope == 1 && lc.hasSuffix("init"))
                    || (scope == 2 && lc.hasSuffix("high"))) {
                    self.ctrlsel.append(ctrl)
                }
            }
        }
    }
    

    // self.searchDisplay.searchResultsTableView.reloadData()
}

}

Crash at Appdelegate. In the swift3 and Xcode 8.0 environment. To convenient get search result ,use Search Bar and Search Display Controller in storyboard.

When I run my application, it crash at :class AppDelegate: UIResponder, UIApplicationDelegate this line in Appdelegate.swift.The break info is : Thread 1: breakpoint 2.1

EDIT:

after set dataSource and delegate of tableView to vc: there comes a strange scenario:the searchbar is under the tableView


Solution

  • I believe you have set the datasource and the delegate of the tableview incorrectly, please set the datasource and delegate either in the interface builder as shown in the below screenshot,

    Select the tableview and set the data source in the connection inspector

    Select the tableview and set the data source in the connection inspector.

    Or you can do this via code, by setting them in your viewDidLoad() method. Paste these lines in viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    

    As I can see from your code snippet that you've pasted, you've already implemented the UITableViewDelegate & UITableViewDataSource methods. But you've not mentioned that your class conforms to these protocols so modify your class declaration and specify that your class conforms to these protocols.

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    //Your class implementation goes here
    }