user3033437 user3033437 - 3 months ago 20
Swift Question

How to dequeue correct custom cell type in tableview:cellForRow:AtIndexPath

I'm doing a SearchBar and when you type the word "nike" it has to display custom cells "shop Types" that has that name in their title but also the brand nike if it exists in the brands array.

For the moment I manage to get the correct numbers of rows in table view:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count + filteredBrands.count
}
return shops.count;
}


Also managed to filter brands and shops arrays:

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()
}


But now I'm stuck in
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
}
}


I really don't have a clue on how to dequeue the correct cell and I'm having weird errors when reuse cells. Can anybody help?

Answer

I think that this example can help you

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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(section == 0)
       {
         return filtered.count
       }

      return filteredBrands.count
   }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                   var tableCell2 : UITableViewCell
    if(indexPath.section == 0)
    {
        tableCell2 = tableView.dequeueReusableCellWithIdentifier("ShopCell") as! ShopCell
    }else
    {
        tableCell2 = tableView.dequeueReusableCellWithIdentifier("BrandsCell") as! BrandsCell
    }

      return tableCell2
   }

I hope this helps you