我的搜索栏有搜索栏控制器和表格视图及其工作。
图像:
以下是代码:
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的所有偏好。
任何想法,我该怎么做?