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

Uitableview 选择行后不重新加载表视图,uitableview,swift,cell,uisearchdisplaycontroller,Uitableview,Swift,Cell,Uisearchdisplaycontroller,我对searchDisplayController和自定义原型单元有问题。我的故事板有一个带有Tulona标识符的自定义原型单元的表视图。故事板也有一个搜索显示控制器。当搜索栏聚焦时,我想将一些搜索选项显示为基本单元格,选择一个选项后,我想用该自定义单元格重新加载表格。我申请的代码如下。一切都很完美,除了选择选项后的表视图刷新/重新加载不完美。当我从显示控制器的表视图中选择一个选项时,带有Tulona单元的主表视图情节提要表不会出现。如果我在搜索栏中单击“取消”按钮,则主表视图会显示自定义单元格

我对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")
        }
    }   
}

通过将这些行添加到didSelectRowAtIndexPath函数中,我已经解决了我的问题-

dispatch_async(dispatch_get_main_queue()){
                self.tableView.reloadData()
                self.searchDisplayController!.setActive(false, animated: true)
                println("Table view should reload")
            }

我确实用这些代码解决了我的问题,但我的表视图单元格总是填充与数组self.brandsToCompare的第一个索引相同的数据。我如何解决这个问题?请帮帮我。