i have a bit of a weird issues (seems opposite to what is documented). book nominates instance variability;r to be true and delegate method to be false… this won’t work with my code as on clean start to iPad it says “no results found”. however if i swap them round… variable true and delegate false it all works as it should? is this a book error or have i typed something wrong somewhere else that makes things go opposite? i’ve redone and redone (even from a few pages back) and i still get the opposite only working.
thanks
zoran
…my code below (which works but is different to book).
import UIKit
class SearchViewController: UIViewController {
// outlets for search bar
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
// variable to test whether a search has been done or not at startup
var hasSearched = true // supposed to be set to "true" in the search bar delegate/function and false here???
// the model for the table view, an instance array
var searchResults = [SearchResult]()
override func viewDidLoad() {
super.viewDidLoad()
// use the table view content inset attributes. this tells table view to add the nominated margins
tableView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
}
}
//EXTENSIONS…
// getting the keyboard button to do something… output the written search term from search bar to XCode console.
extension SearchViewController: UISearchBarDelegate {
// clicked to search item function
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
// releases the keyboard after search
searchBar.resignFirstResponder()
// searchbar delegate will place fake data into the searchResults array
searchResults = []
// for testing… if justin beeber not selected then follow through with search.
if searchBar.text != “justin beeber” {
// repeats the fake data 3 times so there is more than one entry
for i in 0…2 {
let searchResult = SearchResult()
searchResult.name = String(format: “Fake Result %d for”, i)
searchResult.artistName = searchBar.text!
searchResults.append(searchResult)
}
}
// sets the boolean value as "has been searched’.
hasSearched = false
tableView.reloadData()
print(“The search text is: ‘(searchBar.text!)’”)
}
// unify the top of the search bar with the status bar so there is no white gap.just looks neater
func position(for bar: UIBarPositioning) → UIBarPosition {
return .topAttached
}
}
// showing fake results
extension SearchViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) → Int {
// check to see if search has been done
if hasSearched {
return 0
// to make sure the search follows through on a nonitem (beeber) add 1 to the count.
} else if searchResults.count == 0 {
return 1
} else {
// returns all search results in the array
return searchResults.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "SearchResultCell"
var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
// rather than a regular table view cwll the code now uses a "subtitle" cell style... the artistName property goes into the subtitle cell label.
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
}
// if no search results are found... in our case for testing if "justin beeber" is searched for
if searchResults.count == 0 {
cell.textLabel!.text = "(Nothing Found)"
cell.detailTextLabel!.text = ""
} else {
let searchResult = searchResults[indexPath.row]
cell.textLabel!.text = searchResult.name
cell.detailTextLabel!.text = searchResult.artistName
}
return cell
}
}