删除它们时,UISearchController不会删除结果

时间:2016-02-26 08:00:47

标签: ios swift parse-platform uisearchcontroller uisearchresultscontroller

我在我的应用中实现了UISearchController,以搜索要关注和取消关注的用户。我使用swift和Parse作为后端。问题是,每当我想输入内容然后删除它时,它会保留检索到的结果并且不会删除它们。当没有结果时,表视图显示检索结果的“无结果”,因为没有删除!! 我无法弄清楚问题是什么。有帮助吗?

这是代码,

UISearchController委托和方法,

// To search
 var userSearchController: UISearchController!
 var resultsController = UITableViewController()
 var searchActive: Bool = false

// To follow and unfollow users
var userIds = [""]
var isFollowing = ["":false]


// MARK: - Lifecycle

override func viewDidLoad() {
    super.viewDidLoad()

    // tableview delegate and datasource
    self.resultsController.tableView.dataSource = self
    self.resultsController.tableView.delegate = self

    self.resultsController.loadViewIfNeeded()
    configureSearchController()
}

func configureSearchController(){

    self.userSearchController = UISearchController(searchResultsController: nil)
    // Set the search controller to the header of the table
    self.tableView.tableHeaderView = self.userSearchController.searchBar
    // This is used for dynamic search results updating while the user types
    // Requires UISearchResultsUpdating delegate
    self.userSearchController.searchResultsUpdater = self
    self.userSearchController.dimsBackgroundDuringPresentation = false
    self.definesPresentationContext = true

    // Configure the search controller's search bar
    self.userSearchController.searchBar.placeholder = "Search for a parent"
    self.userSearchController.searchBar.sizeToFit()
    self.userSearchController.searchBar.delegate = self
}

// MARK: - Search Bar Delegate Methods

func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    let searchString: String = searchBar.text!.lowercaseString
    if searchActive == false {
        searchActive = true
        // Force search if user pushes button
        if (searchString != "") {
            loadSearchUsers(searchString)
        }

        self.resultsController.tableView.reloadData()

    }
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchActive = true

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchActive = false

}


func searchBar(searchBar: UISearchBar, textDidChange searchText: String){

    if (searchBar.text == ""){
        searchActive = false
    }
    else {
        searchActive = true
        let searchString: String = searchBar.text!.lowercaseString
        if (searchString != ""){
            loadSearchUsers(searchString)}
    }

}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    searchActive = false
    searchBar.text = ""
    self.searchUsers.removeAll(keepCapacity: false)

    self.resultsController.tableView.reloadData()

}

func updateSearchResultsForSearchController(searchController: UISearchController) {

    if let searchString: String = searchController.searchBar.text!.lowercaseString {

        if (searchString != "" && !self.searchActive) {
            loadSearchUsers(searchString)
        }
    }
}

这是numberOfSectionsInTableView

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    var numOfSection: NSInteger = 0

     if userIds.count > 0 {
        //self.resultsController.tableView.reloadData()
        self.tableView.backgroundView = nil
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        numOfSection = 1


    } else {

        let noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
        noDataLabel.text = "No result."
        noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 180.0/255.0, alpha: 1.0)
        noDataLabel.textAlignment = NSTextAlignment.Center
        self.tableView.backgroundView = noDataLabel
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }

    return numOfSection
}

如果您需要更多信息,请与我们联系。非常感谢你。

2 个答案:

答案 0 :(得分:0)

表格数据不会因为您在搜索文本中进行更改而被删除。您必须先清除数据集,然后将搜索结果设置为数据集,然后重新加载,您已经在searchBarCancelButtonClicked方法中执行此操作。

self.searchUsers.removeAll()
self.searchUsers.appendContentsOf(results)
self.resultsController.tableView.reloadData()

答案 1 :(得分:0)

也许你忘记了table.reloadData()。试试这段代码

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var table: UITableView!

var dataArray = [1 : "first" ,2 : "second",3 :"third",]
var filteredArray = [String]()
var gotResult = false

override func touchesBegan(touches: Set<UITouch>, withEvent event:   UIEvent?)
{
    self.view.endEditing(true);
    self.searchBar.endEditing(true)
    gotResult = false
    table.reloadData()
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
{
    gotResult = true
    table.reloadData()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
    if !gotResult
    {
        gotResult = true
        table.reloadData()
    }
    searchBar.resignFirstResponder()
}

func searchBar(searchBar: UISearchBar, textDidChange searchedText: String)
{

    filteredArray = dataArray.values.filter({ (Items) -> Bool in
        let currentItem: NSString = Items

        return (currentItem.rangeOfString(searchedText, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
    })

    self.table.reloadData()
}
//Codes for table 
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if gotResult {
        return filteredArray.count
    }
    else {
        return dataArray.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell();
    let value = Array(dataArray.values)[indexPath.row]

    if gotResult {
        cell.textLabel?.text = filteredArray[indexPath.row]
    }
    else {
        cell.textLabel?.text = value

    }

    return cell
}