行选择后不会重新加载表视图

时间:2015-02-09 08:11:24

标签: uitableview swift cell uisearchdisplaycontroller

我遇到了searchDisplayController和自定义原型单元的问题。我的故事板有一个表格视图,其中包含一个定制的原型单元格,其中包括" Tulona"标识符。故事板还有一个搜索显示控制器。当搜索栏聚焦时,我想将一些搜索选项显示为基本单元格,在选择一个选项后,我想用该自定义单元格重新加载表格。我应用如下代码。除了选择一个选项没有完美刷新/重新加载后的表视图,一切都很完美。当我从显示控制器的表格视图中选择一个选项时,主表格视图(带有" Tulona"单元格的故事板表格)不会出现。如果我从搜索栏单击取消按钮,主表视图确实显示自定义单元格。请告诉我为什么在从显示控制器表视图中选择选项后主表没​​有重新加载?我是iOS开发的新手。如果这是非常明显或容易的事情我很抱歉,我只是错过了它。如果是的话,请指出正确的方向..谢谢。

class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate {
    var brandCollection : [BrandListItem]?
    var aliaseCollection : [BrandAliaseItem]?
    var brandsToCompare = [Brand]()
    var currentSearchDataArray = [String]()

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

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

        //var nib = UINib(nibName: "SearchTableViewCell", bundle: nil)
        //self.searchDisplayController!.searchResultsTableView.registerNib(nib, forCellReuseIdentifier: "SearchOptions")

        let dataManager = DataManager.sharedInstance
        self.brandCollection = dataManager.brandCollection
        self.aliaseCollection = dataManager.aliaseCollection     
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool{
        println(searchString)

        if !searchString.isEmpty{
            //Clearing current search data array for new search key filtering
            self.currentSearchDataArray.removeAll(keepCapacity: false)

            //Add all brand matching to new search key from brand collection to the current search data array
            for brand in self.brandCollection! {
                if brand.name.lowercaseString.rangeOfString(searchString.lowercaseString) != nil {
                    self.currentSearchDataArray.append(brand.name)
                }
            }

            //Add a brand matching to new search key from aliase collection to the current search data array by avoiding duplication
            for aliase in self.aliaseCollection! {
                if aliase.aliaseName.lowercaseString.rangeOfString(searchString.lowercaseString) != nil {
                    var brandName = self.isBrandExist(aliase.brandId)
                    if !brandName.isEmpty{
                        if !contains(self.currentSearchDataArray, brandName){
                            self.currentSearchDataArray.append(brandName)
                        }
                    }
                }
            }
        }

        return true
    }


    //Return brand name if brand id and aliase id is same, otherwise empty
    func isBrandExist(id:Int)->String{
        for brand in self.brandCollection! {
            if id == brand.id {
                return brand.name
            }
        }
        return ""
    }


    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            return self.currentSearchDataArray.count
        } else {
            return self.brandsToCompare.count
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("SearchOptions") as UITableViewCell
        let cell = UITableViewCell()
        // Check to see whether the normal table or search results table is being displayed and set the Brand object from the appropriate array
        if tableView == self.searchDisplayController!.searchResultsTableView {
            let brandName = self.currentSearchDataArray[indexPath.row] as NSString
            cell.textLabel?.text = brandName
            //cell.tag = brand.id
        } else {
            let cell: TulonaCell = self.tableView.dequeueReusableCellWithIdentifier("Tulona", forIndexPath: indexPath) as TulonaCell
            var brand = self.brandsToCompare[indexPath.row] as Brand
            cell.setCustomCellForTulona(brand.name, rating: brand.rating, NoOfComment: brand.no_of_comments)
        }

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if tableView == self.searchDisplayController!.searchResultsTableView {
            self.brandsToCompare.append(Brand(id: 100, rating: 5, no_of_comments: "110", name: "Wolf", aliases: []))
            self.tableView.reloadData()
            self.searchDisplayController!.searchBar.resignFirstResponder()
        }else{
            println("Table view should not reload")
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

我已经通过将这些行添加到didSelectRowAtIndexPath函数 -

来解决了我的问题
dispatch_async(dispatch_get_main_queue()){
                self.tableView.reloadData()
                self.searchDisplayController!.setActive(false, animated: true)
                println("Table view should reload")
            }