我的表格视图目前看起来像这样:
注意:我实际上已经模糊了名字。
我导入一个json文件并从中检索名称。名称显示在表格视图中。在我的ViewController中,如果json文件中的位置字段为nil,则返回一个空单元格。
如何删除空单元格?
我试过了
tableView.tableFooterView = UIView()
但这不起作用
这是我的ViewController类:
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 < $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) -> 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 (searchController.active){
return self.filteredContacts.count
}else{
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 (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) -> Bool in
//filters contacts array
var categoryMatch = (scope == "Title")
var stringMatch = contact.name?.rangeOfString(searchText)
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 = cell.contact
}
}
}
答案 0 :(得分:4)
您需要过滤掉没有位置的
func filterList() { // should probably be called sort and not filter
contacts.sort() { $0.name < $1.name } // sort the fruit by name
contacts = contacts.filter { $0.location != nil }
//tableView.reloadData(); // notify the table view the data has changed
}