我有工作搜索栏,当我将它移动到导航栏时,它停止工作 swift

I have working search bar, when i move it to navigation bar, it stopped working swift

我有带搜索栏控制器的搜索栏和 table 视图及其工作。

图片:

代码如下:

class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate {

@IBOutlet var searchBar: UISearchBar!
@IBOutlet var tableView: UITableView!
var friendsArray = [FriendItem]()
var filteredFriends = [FriendItem]()

override func viewDidLoad() {
    super.viewDidLoad()

    //var leftNavBarButton = UIBarButtonItem(customView:searchBar)
    //self.navigationItem.leftBarButtonItem = leftNavBarButton

    self.friendsArray += [FriendItem(name: "Test1")]
    self.friendsArray += [FriendItem(name: "Test2")]
    self.friendsArray += [FriendItem(name: "Test3")]

    self.tableView.reloadData()

    //var leftNavBarButton = UIBarButtonItem(customView: searchBar)
    //self.navigationItem.leftBarButtonItem = leftNavBarButton
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if (tableView == self.searchDisplayController?.searchResultsTableView)
    {
        return self.filteredFriends.count
    }
    else
    {
        return self.friendsArray.count
    }
}

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

    var friend : FriendItem

    if (tableView == self.searchDisplayController?.searchResultsTableView)
    {
        friend = self.filteredFriends[indexPath.row]
    }
    else
    {
        friend = self.friendsArray[indexPath.row]
    }

    cell.textLabel?.text = friend.name

    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    var friend : FriendItem

    if (tableView == self.searchDisplayController?.searchResultsTableView)
    {
        friend = self.filteredFriends[indexPath.row]
    }
    else
    {
        friend = self.friendsArray[indexPath.row]
    }

    println(friend.name)   
}

func filterContetnForSearchText(searchText: String, scope: String = "Title")
{
    println(searchText)

    self.filteredFriends = self.friendsArray.filter({( friend : FriendItem) -> Bool in
        var categoryMatch = (scope == "Title")

        var stringMatch = friend.name.rangeOfString(searchText)

        return categoryMatch && (stringMatch != nil)
    })
}

func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool   
{
    self.filterContetnForSearchText(searchString, scope: "Title")
    return true
}

func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool
{        self.filterContetnForSearchText(self.searchDisplayController!.searchBar.text, scope: "Title")

    return true   
}
}

我需要将该搜索栏移动到导航栏,我必须看起来像这样:

我发现了这个:

var leftNavBarButton = UIBarButtonItem(customView: searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton

但是我的搜索完全停止了。我认为它失去了对 searchBar 的所有偏好。

知道吗,我该怎么做?

我有。

    var searchBars:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))

并在 viewDidload 中

 var leftNavBarButton = UIBarButtonItem(customView: searchBars)
 self.navigationItem.leftBarButtonItem = leftNavBarButton