你好,我在一些应用程序中看到,它们在表格 View 单元格上显示微调器或事件指示器,当用户搜索某些内容时,该单元格尚未填充值。这很难解释,但就像如果我在搜索栏中输入一些内容,结果就会根据我输入的关键字开始在表格 View 上弹出,并且旋转器开始在单元格上移动。如果前两个值来得快,它会在第三个单元格上显示微调器,只要值开始填充单元格,微调器就会转到下一个空单元格。怎样才能有这样的效果呢。这是我搜索城市和国家的代码
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()
}
}
}
}
}
这是我需要的具有这样效果的应用程序之一
最佳答案
我所做的是以编程方式添加一个 subview ,因此主表在后台,而微调器是集中的。这个例子实际上是在 Objective-C 中,但你明白了。它位于 viewDidLoad
方法中:
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];
关于ios - 如何在 tableView 加载单元格上显示微调器/指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811868/