AlmoDev
AlmoDev

Reputation: 969

fatal error: Index out of range two custom cells

I have two custom cells enter image description here

I configured them like this

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if searchActive{
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
        let entry1 = auxiliar?[indexPath.row]
        cell.shopImage.hnk_setImage(from: URL(string: (entry1?.Logo)!))
        cell.shopName.text = entry1?.shopname

        cell.backgroundColor = UIColor(white: 1 , alpha : 0.5)
        return cell
    } else {
        if filteredArr == nil {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell

            let entry = shops[indexPath.row]

            cell.shopImage.hnk_setImage(from: URL(string: entry.Logo))
            cell.shopName.text = entry.shopname

            cell.backgroundColor = UIColor(white: 1 , alpha : 0.5)
            return cell
        } else {
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "design", for: indexPath) as! DesignTableViewCell
            let entry2 = filteredArr[indexPath.row]// error here 

            cell2.deignImage.hnk_setImage(from: URL(string: "http://jaee.com/upload/img/\(entry2.design)"))

            cell2.backgroundColor = UIColor(white: 1 , alpha : 0.5)
            return cell2
        }
    }
}

but I get an error that index out of range. Can someone help me. I think it's because of numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchActive {
        return auxiliar!.count
    }
    return allShops.count
} 

and here is how I save data. I'm filtering the shops. The shops with a design they should be in the design cell and the rest should be in Cell.

var info = Shops(shopname: shopName, Logo: logoString, family_id: familiy_id , design : designImage )

self.shops.append(info)
self.filteredArr = self.shops.filter { $0.design != nil }
self.NofilteredArr =  self.shops.filter { $0.design == nil }
self.allShops = self.NofilteredArr + self.filteredArr

UPDATE What I want is the design cell to be on the top only and the Cell is below it. enter image description here

Upvotes: 0

Views: 205

Answers (2)

Ivan
Ivan

Reputation: 294

In the cellForRowAt: method you have:

if searchActive{
    ...
} else {
    if filteredArr == nil {
        ...
    } else {
        ...
    }
}

But in the numberOfRowsInSection: you have

if searchActive {
    ...
}
return ...

Probably you'll want to implement the same logic in the both methods.

Upvotes: 0

Dixit Akabari
Dixit Akabari

Reputation: 2547

Try this.

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchActive {
            return auxiliar!.count
        }else{
             if filteredArr == nil {
                shops.count
             }else{
                filteredArr.count
            }
        }
    }

i think your allShops is nil.

Upvotes: 2

Related Questions