gpt4 book ai didi

ios - 如何在表格 View 中出队正确的自定义单元格类型:cellForRow:AtIndexPath

转载 作者:行者123 更新时间:2023-11-30 13:08:36 25 4
gpt4 key购买 nike

我正在做一个搜索栏,当您输入“nike”一词时,它必须显示自定义单元格“shop Types”,该单元格的标题中包含该名称,而且还包含品牌 nike(如果品牌数组中存在该品牌)。

目前我设法在表格 View 中获取正确的行数:

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

我真的不知道如何使正确的单元出队,并且在重用单元时遇到奇怪的错误。有人可以帮忙吗?

最佳答案

我认为这个例子可以帮助你

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
}

希望对你有帮助

关于ios - 如何在表格 View 中出队正确的自定义单元格类型:cellForRow:AtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39018383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com