0

我在我的應用程序中實現了UISearchController,以搜索用戶關注並取消關注。我使用swift和Parse作爲後端。問題是,無論何時我想輸入內容然後刪除它,它都會保留檢索結果並不會刪除它們。當沒有結果時,表格視圖顯示檢索結果爲「無結果」,因爲沒有刪除! 我找不出什麼問題。任何幫助?當我刪除它時,UISearchController不會刪除結果

下面是代碼,

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 
} 

讓我知道如果你需要更多的信息。非常感謝。

+0

@Russell hi Russell!我從這裏得到了你的UISearchController,http://stackoverflow.com/questions/32356676/smart-search-for-parse-usernames-in-swift-not-working併爲我工作,但我有這個問題,你會幫助嗎?我 ? –

回答

0

表格數據不會因爲您在搜索文本中進行更改而被刪除。您必須首先清除數據集,然後將搜索結果設置爲數據集,然後重新加載,您已在searchBarCancelButtonClicked方法中執行此操作。

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

這是什麼意思? self.searchUsers.appendContentsOf(results) –

+0

你介意我發給你演示嗎?這個問題讓我很失望! –

+0

@TejasArdeshna你認爲我只能使用UISearchController來搜索我的應用程序中已有的數據嗎?我是否需要在我的應用中使用所有數據來通過它們進行篩選? –

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 
} 
+0

此代碼沒有UISearchUpdateResult? –

+0

是的,它更新結果reloadingData() –

+0

謝謝你的回覆!但我有問題。你認爲我只能使用UISearchController在我的應用程序中搜索嗎? –