在 table 视图控制器 iOS swift 中搜索特定记录

search a particular record in table view controller iOS swift

我正在使用 jsonserialization 方法从 url 获取数据,并将每条记录存储在 class 的对象中,并在 table vc 中显示每条记录。

现在我想使用搜索栏实现搜索并观看许多比较字符串类型数组和另一个字符串类型数组(数组中的硬编码值)的教程。 但根据我的需要,我的数据存储在 class obj 中而不是硬编码数据,并且来自该对象的数据显示在 cellforrow 上。 cellforrow 包含。

if isSearching 
{
    cell.textLabel?.text = searchArray[indexPath.row] //.compName

    cell.detailTextLabel?.text = searchArray[indexPath.row]
}
else{ 
    cell.textLabel?.text = tempArray[indexPath.row].compName

    cell.detailTextLabel?.text = tempArray[indexPath.row].complocation
}
return cell

那么如何实现搜索栏,出现以下错误。 请帮助! 这里我的 classname 是 -> comp。 searchArray 是字符串数组, tempArray 是包含 class obj

的数组

Image of the error

那么您只需将您使用的搜索过滤器修改为:

searchArray = tempArray.filter({[=10=].compName == searchBarClicked.text!})

编译器告诉你不能比较 comp 类型的 [=11=]String 类型的 searchBarClicked.text,所以 [=15=].compName 将是 String 因此可以使用 ==

找到了。这是 以下是 else 条件的变化

searchArr   = tempArray.filter { [=10=].<classAttribute>.contains(searchText) }

searchArray = searchArr as! [comp]

print("search match")

listTableView.reloadData()

这里的searchArr和searchArray和tempArray是class类型的数组,& class属性是您要在表视图中搜索的 class 的属性,我的 class 名称是 "comp"

使用这个:

searchArray = searchArray.filter{(([=10=]["name"]?.localizedCaseInsensitiveContains(textField.text!)))!}