UISearchBar 没有更新

Posted

技术标签:

【中文标题】UISearchBar 没有更新【英文标题】:UISearchBar not updating 【发布时间】:2015-11-30 18:41:10 【问题描述】:

我有一个UIViewController,我在其中添加了UITableViewController,我正在以编程方式添加搜索栏。我可以在控制台上搜索后打印结果,但是当我点击搜索栏时,自定义表格单元格仍会被填充,并且搜索根本不会更新它们。

我在override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 中有打印语句来检查搜索控制器是否处于活动状态并且它从不打印该语句。

import UIKit
import Parse

class MasterViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate 

@IBOutlet var mTableView: UITableView!
var detailViewController: DetailViewController? = nil
var objects = [AnyObject]()
var resultSearchController = UISearchController()
var filteredData : NSMutableArray = []
var sectionInTable : NSMutableArray = ["Grand Haven 9", "Holland 7"]
var movieTitle : NSMutableArray = ["The Hunger Game 2","Creed"]
var movieTimeSection1 : NSMutableArray = ["11:00am, 11:40, 12:10pm, 1:25, 2:30, 6:05, 6:55, 7:30, 9:05", "11:10am, 11:50, 12:20pm, 2:25, 3:30, 6:05, 6:55, 8:30, 10:05"]
var movieTimeSection2 = ["Title": "Martian", "Theatre": "Holland 7", "Time": "11:00am, 11:40, 12:10pm, 1:25, 2:30, 6:05, 6:55, 7:30, 9:05"]


var onlineMovieTitle : NSMutableArray = []

var movieSection1 : NSMutableArray = []
var movieSection2 : NSMutableArray = []

override func viewDidLoad() 
    super.viewDidLoad()

    print(movieSection1)
    print(movieSection2)

    self.tableView.separatorColor = UIColor.cloudsColor()
    self.tableView.backgroundColor = UIColor.cloudsColor()

    self.resultSearchController = (
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.delegate = self
        controller.searchBar.sizeToFit()
        controller.preferredStatusBarStyle() == UIStatusBarStyle.LightContent
        controller.hidesNavigationBarDuringPresentation = false
        self.mTableView.tableHeaderView = controller.searchBar

        return controller
    )()
    //self.resultSearchController.searchBar.endEditing(true)


func updateSearchResultsForSearchController(searchController: UISearchController)
    filteredData.removeAllObjects()

    let searchPredicate = NSPredicate(format: "Title CONTAINS %@", searchController.searchBar.text!.uppercaseString)
    let array = (movieSection1 as NSArray).filteredArrayUsingPredicate(searchPredicate)
    let array2 = (movieSection2 as NSArray).filteredArrayUsingPredicate(searchPredicate)
    //let array2 = (movieTimeSection1 as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredData.addObjectsFromArray(array)
    filteredData.addObjectsFromArray(array2)

    print(filteredData)


override func viewWillAppear(animated: Bool) 
    super.viewWillAppear(animated)
    self.resultSearchController.searchBar.hidden = false


// MARK: - Segues

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    if segue.identifier == "showDetail" 
        if let indexPath = self.tableView.indexPathForSelectedRow 

        

    


// MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    return 2


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    var count : Int = 0
    if (self.resultSearchController.active) 
        return 2
    
    else 
        if section == 0 
            count = movieSection1.count
         else if section == 1 
            count = movieSection2.count
        
    
    return count



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    let cell : MainTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MainTableViewCell

    //Fancy Cells
    var corners : UIRectCorner = UIRectCorner.AllCorners
        if (tableView.numberOfRowsInSection(indexPath.section) == 1) 
            corners = UIRectCorner.AllCorners
         else if (indexPath.row == 0) 
            corners = UIRectCorner.TopLeft.union(UIRectCorner.TopRight)
         else if (indexPath.row == tableView.numberOfRowsInSection(indexPath.section) - 1) 
            corners = UIRectCorner.BottomLeft.union(UIRectCorner.BottomRight)
        
    cell.configureFlatCellWithColor(UIColor.greenSeaColor(), selectedColor: UIColor.cloudsColor(), roundingCorners: corners)

    //Fancy Buttons
    cell.cornerRadius = 7
    if indexPath.row == 0 
        cell.trailorButton.addTarget(self, action: "openHunger:", forControlEvents: UIControlEvents.TouchUpInside)
    else if indexPath.row == 1
        cell.trailorButton.addTarget(self, action: "openCreed:", forControlEvents: UIControlEvents.TouchUpInside)
    
    cell.trailorButton.layer.cornerRadius = 5
    cell.trailorButton.tintColor = UIColor.whiteColor()

    let movieNameStr = movieTitle[indexPath.row] as! String
    cell.movieImage.image = UIImage(named: movieNameStr)

    //Cell Data Insertion

    if (self.resultSearchController.active)
        print("search ---------------")
        cell.movieName.text = filteredData[indexPath.row].valueForKey("Title") as? String
        cell.movieDetail.text = filteredData[indexPath.row].valueForKey("Time") as? String
        return cell
    else 
        print("cell")
        if indexPath.section == 0 
            cell.movieName.text = movieSection1[indexPath.row].valueForKey("Title") as? String
            cell.movieDetail.text = movieSection1[indexPath.row].valueForKey("Time") as? String
         else if indexPath.section == 1 
            cell.movieName.text = movieSection2[indexPath.row].valueForKey("Title") as? String
            cell.movieDetail.text = movieSection2[indexPath.row].valueForKey("Time") as? String
        
        return cell
    



    //return cell


@IBAction func openHunger(sender: AnyObject) 
    UIApplication.sharedApplication().openURL(NSURL(string: "https://youtu.be/n-7K_OjsDCQ")!)

@IBAction func openCreed(sender: AnyObject) 
    UIApplication.sharedApplication().openURL(NSURL(string: "https://youtu.be/fCBzWLVQgk8")!)

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
    return self.sectionInTable[section] as? String



任何帮助将不胜感激。谢谢

【问题讨论】:

Dereck .. 在“updateSearchResultsForSearchController”中折腾其中之一 dispatch_async(dispatch_get_main_queue(), ^ [self.contentView.tableView reloadData]; ); .. 这是objective-c,所以只需将它翻译成Swift。但我认为您可能只需要重新加载 tableView 即可查看结果,但我到底知道,我不使用 UISearchControllers 或其他任何名称,我使用 UITextFields 并手动完成所有操作。 天啊,我不敢相信我删除了那行,谢谢它是问题所在。我不敢相信我看了很多遍之后错过了它。您想将此作为答案发布,以便我关闭它吗? 没问题,希望这是唯一的问题 【参考方案1】:

我错过了,感谢 Larcerax,通过添加 mTableView.reloadData() 解决了问题

【讨论】:

以上是关于UISearchBar 没有更新的主要内容,如果未能解决你的问题,请参考以下文章

UISearchBar 在尝试选择 UICollectionViewCell 时退出第一响应者

UISearchBar 没有响应

UISearchBar 在搜索没有结果时显示标签,但在单击 UISearchBar 中的 x 时不会再次隐藏标签

当我在 NavigationBar 中单击 UISearchBar 时没有任何反应

UISearchBar 在 iOS 11 中增加导航栏高度

UISearchBar 没有出现