How do I remove empty cells in table view? iOS Swift

My table view currently looks like this:

Contact Table View

  • add additional buttons in UITableView swipe
  • didDeselectRowAtIndexPath indexPath is nil
  • Keep getting nil from dequeueReusableCellWithIdentifier?
  • Change the Width of UISearchBars on a TableView
  • Asynchronously set images in tableview
  • View-Based NSTableView in Swift - How to
  • Note: I have blurred out actually names.

    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.

    How do I remove the empty cells?

    I have tried

    tableView.tableFooterView = UIView()

    But that does not work

    Here is my ViewController class:

    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() {
        //initialize the defaults manager class
        //search controller
        searchController = UISearchController(searchResultsController: nil)
        tableView.tableHeaderView = searchController.searchBar
        definesPresentationContext = true
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        //load the contacts
        title = "Contacts"
        contacts = [Contact]()
        let api = ContactAPI()
    override func viewDidAppear(animated: Bool) {
        //reload the table view to check for new contacts
        //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
    func filterList() { // should probably be called sort and not filter
        contacts.sort() { $ < $ } // sort the fruit by name
        //tableView.reloadData(); // notify the table view the data has changed
    func didLoadContacts(contacts: [Contact]){
        self.contacts = contacts
    //MARK: -Table View
    //set number opf sections in table view
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    //delegeate that tells tabel view how many cells to have
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return size of array
        //if searching show count of filtered contacts
        if ({
            return self.filteredContacts.count
            return self.contacts.count
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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 ({
            contact = self.filteredContacts[indexPath.row]
            contact = self.contacts[indexPath.row]
        if contact.location == "nil"{
            return cell
        //handel assignment of text
        cell.textLabel?.text =
        //retrieve from customcell class = contact
        return cell
    //MARK: -Search
    func filterContentForSearchText(searchText: String, scope: String = "Title")
        self.filteredContacts = self.contacts.filter({( contact: Contact) -> Bool in
            //filters contacts array
            var categoryMatch = (scope == "Title")
            var stringMatch =
            return categoryMatch && (stringMatch != nil)
    func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchString searchString: String!) -> Bool {
        self.filterContentForSearchText(searchString, scope: "Title")
        return true
    func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchScope searchOption: Int) -> 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 =

    Solutions Collect From Internet About “How do I remove empty cells in table view? iOS Swift”

    You need to filter out the ones that don’t have a location

    func filterList() { // should probably be called sort and not filter
      contacts.sort() { $ < $ } // sort the fruit by name
      contacts = contacts.filter { $0.location != nil }
      //tableView.reloadData(); // notify the table view the data has changed