我按照这个配置了它们
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
}
}
}
但是我得到一个索引超出范围的错误。有人能帮我吗。我认为这是因为numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return auxiliar!.count
}
return allShops.count
}
以下是我保存数据的方式。我正在过滤商店。具有设计的商店应该在design
单元格中,其余的应该在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
答案 0 :(得分:2)
试试这个。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return auxiliar!.count
}else{
if filteredArr == nil {
shops.count
}else{
filteredArr.count
}
}
}
我认为你的allShops是零。
答案 1 :(得分:0)
在cellForRowAt:方法中,你有:
if searchActive{
...
} else {
if filteredArr == nil {
...
} else {
...
}
}
但是在numberOfRowsInSection中:你有
if searchActive {
...
}
return ...
可能你会想要在两种方法中实现相同的逻辑。