如何在tableView上显示微调器/指示器加载单元

时间:2016-03-05 08:26:11

标签: ios swift uitableview uiactivityindicatorview

你好,我在一些应用程序中看到过,这些应用程序在tableview单元格上显示微调器或活动指示器,当用户搜索某些内容时,该指示器没有填充值。很难解释,但就像我在搜索栏中输入内容一样,根据我输入的关键字和spinner开始在单元格上移动,结果开始在tableview上弹出。如果前两个值变快,则显示第三个单元格上的微调器,只要值开始填充单元格,微调器就会转到下一个空单元格。我怎么能有这个效果。这是我搜索城市和国家的代码

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()
                }
            }
        }
}
}

这是我需要的具有此效果的应用程序之一

enter image description here

2 个答案:

答案 0 :(得分:1)

我所做的是以编程方式添加子视图,因此主表位于后台,并且微调器是集中式的。这个例子实际上是在Objective-C中,但你明白了。它位于tablesorter方法中:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- choose a theme file -->
<link rel="stylesheet" href="css/theme.default.css">
<!-- load jQuery and tablesorter scripts -->
<script type="text/javascript" src="js/jquery-latest.js"></script>
<script type="text/javascript" src="js/jquery.tablesorter.js"></script>

<!-- tablesorter widgets (optional) -->
<script type="text/javascript" src="js/jquery.tablesorter.widgets.js"></script>
</head>
<body>

<table id="myTable" class="tablesorter">
  <thead>
    <tr>
      <th>Last Name</th>
      <th>First Name</th>
      <th>Email</th>
      <th>Due</th>
      <th>Web Site</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Smith</td>
      <td>John</td>
      <td>jsmith@gmail.com</td>
      <td>$50.00</td>
      <td>http://www.jsmith.com</td>
    </tr>
    <tr>
      <td>Bach</td>
      <td>Frank</td>
      <td>fbach@yahoo.com</td>
      <td>$50.00</td>
      <td>http://www.frank.com</td>
    </tr>
    <tr>
      <td>Doe</td>
      <td>Jason</td>
      <td>jdoe@hotmail.com</td>
      <td>$100.00</td>
      <td>http://www.jdoe.com</td>
    </tr>
    <tr>
      <td>Conway</td>
      <td>Tim</td>
      <td>tconway@earthlink.net</td>
      <td>$50.00</td>
      <td>http://www.timconway.com</td>
    </tr>
  </tbody>
</table>

<script>

$(function(){
      $("#myTable").tablesorter({ sortList: [[0,0], [1,0]] });
    });
</script>
</body>
</html>

答案 1 :(得分:0)

这取决于您正在寻找的效果究竟是什么,但您可以使用微调器作为子视图添加视图(因此您可以将其置于中心位置)作为表格视图页脚。