title: How do I remove empty cells in table view? iOS Swift
body: <p>My table view currently looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/xaT3p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xaT3p.png" alt="Contact Table View"></a></p>
  
  <p>Note: I have blurred out actually names.</p>
  
  <p>I import a json file and retrive names from it.  The names appear on the table view.  In my ViewController I return an empty cell if the location field is nil in the json file.</p>
  
  <p>How do I remove the empty cells?</p>
  
  <p>I have tried </p>
  
  <pre><code>tableView.tableFooterView = UIView()
  </code></pre>
  
  <p>But that does not work</p>
  
  <p>Here is my ViewController class:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UITableViewController, UITableViewDataSource,     UITableViewDelegate, UISearchResultsUpdating {
  
  //manages search bar
  var searchController:UISearchController!
  
  var contacts = [Contact]()
  
  //array to hold contacts that match the search results
  var filteredContacts = [Contact]()
  
  override func viewDidLoad() {
  
      super.viewDidLoad()
  
      //initialize the defaults manager class
      NSUserDefaultsManager.initializeDefaults()
  
      //search controller
      searchController = UISearchController(searchResultsController: nil)
      searchController.searchBar.sizeToFit()
      tableView.tableHeaderView = searchController.searchBar
      definesPresentationContext = true
  
      searchController.searchResultsUpdater = self
      searchController.dimsBackgroundDuringPresentation = false
  
      //load the contacts
      title = "Contacts"
  
      contacts = [Contact]()
      let api = ContactAPI()
      api.loadContacts(didLoadContacts)
  
  }
  
  override func viewDidAppear(animated: Bool) {
      super.viewDidAppear(animated)
  
      //reload the table view to check for new contacts
      tableView.reloadData()
  
      //change nav bar to be more transparent
      navigationController?.navigationBar.alpha = 0.5
  
  }
  
  //MARK: -Helper Methods
  
  // Uupdate searching results
  func updateSearchResultsForSearchController(searchController: UISearchController) {
  
      let searchText = searchController.searchBar.text
      filterContentForSearchText(searchText)
      tableView.reloadData()
  
  }
  
  func filterList() { // should probably be called sort and not filter
      contacts.sort() { $0.name &lt; $1.name } // sort the fruit by name
      //tableView.reloadData(); // notify the table view the data has changed
  }
  
  func didLoadContacts(contacts: [Contact]){
      self.contacts = contacts
      filterList()
      tableView.reloadData()
  }
  
  //MARK: -Table View
  
  //set number opf sections in table view
  override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      return 1
  }
  
  //delegeate that tells tabel view how many cells to have
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      //return size of array
  
      //if searching show count of filtered contacts
      if (searchController.active){
  
          return self.filteredContacts.count
  
      }else{
  
          return self.contacts.count
  
      }
  
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
      let cell = self.tableView.dequeueReusableCellWithIdentifier("customcell") as! CustomCell //from the customcell class
  
      //change color of cell text label
      cell.textLabel?.textColor = UIColor.blackColor()
  
      let contact : Contact
  
      //if users is searching then return filtered contacts
      if (searchController.active){
  
          contact = self.filteredContacts[indexPath.row]
  
      }else{
  
          contact = self.contacts[indexPath.row]
  
      }
  
      if contact.location == "nil"{
          return cell
      }
  
  
      //handel assignment of text
      cell.textLabel?.text = contact.name
  
      //retrieve from customcell class
      cell.contact = contact
  
      return cell
  }
  
  
  //MARK: -Search
  
  func filterContentForSearchText(searchText: String, scope: String = "Title")
  {
      self.filteredContacts = self.contacts.filter({( contact: Contact) -&gt; Bool in
  
          //filters contacts array
  
          var categoryMatch = (scope == "Title")
          var stringMatch = contact.name?.rangeOfString(searchText)
  
          return categoryMatch &amp;&amp; (stringMatch != nil)
  
      })
  }
  
  func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchString searchString: String!) -&gt; Bool {
  
      self.filterContentForSearchText(searchString, scope: "Title")
  
      return true
  
  }
  
  
  func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchScope searchOption: Int) -&gt; Bool {
  
      self.filterContentForSearchText(self.searchController!.searchBar.text, scope: "Title")
  
      return true
  
  }
  
  //MARK: -Segue
  
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  
      if(segue.identifier == "detailview"){
          let cell = sender as! CustomCell
          let detailView = segue.destinationViewController as! DetailViewController
          detailView.preContact = cell.contact
  
      }
  }
  
  }
  </code></pre>