0
我遇到了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")
}
}
}
我没有解决我的问题与那些代码,但我的表视图细胞总是充满了相同的数据的数据数组self.brandsToCompare的第一个索引。我该如何解决这个问题?请帮助我。 – Nuibb 2015-02-10 04:19:19