过滤数组并在表视图中显示? [英] Filtering Array and displaying in Table View?
问题描述
我想在练习字典中搜索名称键,然后在表格视图中显示过滤结果.我正在使用这个功能
I want to search an dictionary of exercises for the name key and then show the filtered result in the table view. I am using this function
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let filtered = exercises.filter { $0["name"] == searchText }
print(filtered)
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.exercisesTableView.reloadData()
}
变量:
var exercises = [Exercise]()
var filtered: [NSMutableArray] = []
var searchActive: Bool = false
在搜索功能中出现错误
Type'Exercise' 没有下标成员
Type'Exercise' has no subscript members
然后我的问题是结果是 NSMutableArray,所以我无法将结果名称设置为要显示的单元格文本
and then i have the issue that the result is an NSMutableArray and so I cant set the result names as cell text to display
无法在强制转换中将NSMutableArray"类型的值转换为String"类型
Cannot convert value of type 'NSMutableArray' to type 'String' in coercion
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if (searchActive){
cell.textLabel?.text = filtered[indexPath.row] as String
} else {
let exercise = exercises[indexPath.row]
cell.textLabel!.text = exercise.name
}
return cell
}
这是我的练习词典以供参考:
Here is my Exercise dictionary for reference:
final public class Exercise {
var id: Int
var descrip: String
var name: String
var muscles: [Int]
var equipment: [Int]
public init?(dictionary: [String: Any]) {
guard
let id = dictionary["id"] as? Int,
let descrip = dictionary["description"] as? String,
let name = dictionary["name"] as? String,
let muscles = dictionary["muscles"] as? [Int],
let equipment = dictionary["equipment"] as? [Int]
else { return nil }
self.id = id
self.descrip = descrip
self.name = name
self.muscles = muscles
self.equipment = equipment
}
我可以通过使 var 过滤来修复第二个错误:[String] = [] 因此它可以用作单元格标题,但这并不能解决第一个错误,我不确定解决它的方法是否正确?
I can fix the second error by making var filtered: [String] = [] so it can be used as a cell title, but that doesnt resolve the first error and im not sure is the right way to go about it?
推荐答案
你的 filtered
也是 [Exercise]
类型,你需要像过滤它一样.
Your filtered
also type of [Exercise]
and you need to filter it like.
var filtered = [Exercise]()
self.filtered = exercises.filter { $0.name == searchText }
这里的$0
是Exercise
对象的类型,因此您需要使用$0.name
访问其属性名称.
Here $0
is type of Exercise
object, so you need to access its property name using $0.name
.
如果您希望 filtered
作为 [String]
的类型,只有名称,那么您可能需要同时使用 map
和 filter
像这样.
If you want filtered
as type of [String]
with only name then you can need to use both map
and filter
like this.
self.filtered = exercises.filter { $0.name == searchText }.map { $0.name }
或
self.filtered = exercises.map { $0.name }.filter { $0 == searchText }
OR 直接使用 filterMap
作为@dfri 建议.
OR directly using filterMap
as @dfri suggested.
self.filtered = exercises.flatMap{ $0.name == searchText ? $0.name : nil }
这篇关于过滤数组并在表视图中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!