0
我正在做一個SearchBar,當您鍵入單詞「nike」時,它必須顯示在其標題中具有該名稱的自定義單元格「shop types」品牌耐克如果它存在於品牌陣列中。如何在tableview中取消正確的自定義單元格類型:cellForRow:AtIndexPath
因爲我設法得到正確的行數在表視圖的那一刻:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count + filteredBrands.count
}
return shops.count;
}
也設法過濾品牌和商店陣列:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filtered = shopNames.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
filteredBrands = brandNames.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
if(filtered.count == 0 && filteredBrands.count == 0){
searchActive = false
} else {
searchActive = true
}
//user pressed the x button to clean content
if(searchText == "") {
searchActive = false
}
currentTableView.reloadData()
}
但現在我被困在tableView:cellForRow:atIndexPath()
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(self.brandNamesCopyCounter > 0){
brandNamesCopyCounter -= 1
let cell = currentTableView.dequeueReusableCellWithIdentifier("brandCell") as! HomeSearchBrandCell
if(searchActive){
if(filteredBrands.count > indexPath.row){
cell.title.text = filteredBrands[indexPath.row]
if(cell.iconFacility != nil){
cell.iconFacility.image = UIImage.init(named: "brands.pdf")
}
}
} else {
print(indexPath.row)
if(brandsArray.count > indexPath.row){
cell.title.text = brandsArray[indexPath.row].name_en
}
if(cell.iconFacility != nil){
cell.iconFacility.image = UIImage.init(named: "brands.pdf")
}
}
return cell
}else{
let cell = currentTableView.dequeueReusableCellWithIdentifier("homeSearchCell") as! HomeSearchCell
if(searchActive){
if(filtered.count > indexPath.row){
print(indexPath.row)
cell.title.text = self.filtered[indexPath.row]
if(cell.iconFacility != nil){
cell.iconFacility.image = UIImage.init(named: "shops.pdf")
}
}
} else {
cell.title.text = shops[indexPath.row].name
if(cell.iconFacility != nil){
cell.iconFacility.image = UIImage.init(named: "shops.pdf")
}
}
return cell
}
}
我真的沒有對如何出隊正確的線索單元格,並且在重用單元格時出現奇怪的錯誤。任何人都可以幫忙嗎?
能否請您解釋是在「isDriver」布爾從何而來? – user3033437
如果我理解你的問題,當你顯示搜索結果時,你需要使用一個單元格或另一個單元格,所以我的isDriver變量是一個布爾變量,僅適用於你的情況下的這個例子,我認爲你需要使用'searchActive'變量,讓我知道如果我錯了 –
不,它更簡單。我需要顯示不同的細胞類型結果。因此,當我處於表格視圖時,我需要區分這些單元格,因爲我無法在品牌單元格中加載商店數據。我需要區分細胞。所以我可以用正確的類 – user3033437