Reputation: 1327
hello I have seen in some apps which shows spinner or activity indicator on a tableview cell which hasn't filled up with a value when user search something. Its hard to explain but it was like if I type something in the search bar,results starting to pops up on the tableview according to the keywords I type and spinner starts moving on the cells. if first two values comes fast it shows the spinner on the third cell, as long as values starting to fill up the cells,spinner goes to next cell which is empty. How can I have this effect. here is my code of searching Cities and countries
class CountryTableViewController: UITableViewController, UISearchResultsUpdating {
var delegate : CountryTableViewControllerDelegate! = nil
var dict = NSDictionary()
var filteredKeys = [String]()
var resultSearchController = UISearchController()
var newTableData = [String]()
var departureOrArrivalSegue:Int?
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredKeys.count
} else {
return dict.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
// let key = self.filteredKeys[indexPath.row]
//let dictionary = self.dict[key] as! NSDictionary
let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)
let stateName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["state_name"] as? NSString)
let shortName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_country_name"] as? NSString)
if (cityName !== "-" || shortName !== "-"){
cell.stateNameLabel.text = stateName as? String
cell.cityNameLabel.text = cityName as? String
cell.shortNameLabel.text = shortName as? String
}
return cell
}else{
if let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString){
cell.cityNameLabel.text = cityName as String
}
return cell
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let id = Int((((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["id"] as?NSString)! as String)
let cityName = (((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)! as String
let countryShortName = (((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["short_country_name"] as?NSString)! as String
delegate.country(id!,cityName: cityName, countryShortName: countryShortName,departureOrArrivalSegue: departureOrArrivalSegue!)
self.navigationController!.popViewControllerAnimated(true)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated);
self.navigationController?.setNavigationBarHidden(true, animated: false)
self.view.layoutIfNeeded()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
}
func getCountriesNamesFromServer(searchWord:String) {
let url:String = "localhost"
let params = ["keyword":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print(result)
self.dict = result
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsCity: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["city_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
let valueContainsCountry: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["country_name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsCity || valueContainsCountry {
self.filteredKeys.append(key as! String)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
}
}
This is the one of the app which has effect like this which I need
Upvotes: 1
Views: 1890
Reputation: 119
What I've done is add a subView programmatically, so the main table is in the background, and the spinner is centralized. This example is actually in Objective-C, but you get the idea. It's in the viewDidLoad
method:
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.spinner.hidesWhenStopped = YES;
self.spinner.frame = CGRectMake(0, 0, 80, 80);
self.spinner.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
[self.view addSubview:self.spinner];
Upvotes: 1
Reputation: 119031
It depends exactly what the effect you're looking for is, but you can add a view with the spinner as a subview (so you can position it centrally) as the table view footer.
Upvotes: 0