过滤数组并在表视图中显示?
我想搜索名称键的练习词典,然后在表格视图中显示过滤结果。我使用这个功能过滤数组并在表视图中显示?
变量:
var exercises = [Exercise]()
var filtered: [NSMutableArray] = []
var searchActive: Bool = false
在搜索功能我得到的错误
Type'Exercise”无标会员
和那么我有问题,结果是一个NSMutableArray,所以我不能设置resul牛逼的名字为单元格文本显示
无法将类型“的NSMutableArray”的价值在强制输入“串”
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
}
这里是我运动的字典以供参考:
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] = []所以它可以用作单元格标题,但是这并不能解决第一个错误,我不知道是否正确的方式去解决它?
你filtered
也类型[Exercise]
,你需要对其进行过滤喜欢。
var filtered = [Exercise]()
self.filtered = exercises.filter { $0.name == searchText }
这里$0
是Exercise
对象的类型,所以你需要使用$0.name
访问其属性名称。
编辑:如果你想filtered
与惟一名称类型的[String]
,那么你可以根据需要同时使用map
和filter
这个样子。
self.filtered = exercises.filter { $0.name == searchText }.map { $0.name }
OR
self.filtered = exercises.map { $0.name }.filter { $0 == searchText }
或直接使用filterMap
作为@dfri建议。
self.filtered = exercises.flatMap{ $0.name == searchText ? $0.name : nil }
这里的问题是$0["name"] == searchText
。 $0
在这种情况下是Excersise
类型,您可以将它称为数组。这条线应该是这样的:
let filtered = exercises.filter { $0.name == searchText }
我昨天告诉你使用自定义的类时忘记字典语义。
请勿使用密钥脚本!
正如在其他的答案中提到必须声明filtered
也为[Exercise]
var filtered = [Exercise]()
,如果你也想搜索部分字符串使用过滤
self.filtered = exercises.filter { $0.name == searchText }
然而
self.filtered = exercises.filter {$0.name.range(of: searchText, options: [.anchored, .caseInsensitive, .diacriticInsensitive]) != nil }
但是
因为这两个过滤和未过滤的阵列型[Exercise]
你必须改变cellForRowAtIndexPath
这样
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let exercise : Exercise
if (searchActive){
exercise = filtered[indexPath.row]
} else {
exercise = exercises[indexPath.row]
}
cell.textLabel!.text = exercise.name
return cell
}
谢谢我试过这个,因为它看起来更干净,保持过滤器相同的类型,没有错误只是即时仍然得到这个奇怪的崩溃时,在搜索栏中键入一个字母,如Nirav的回应评论 – infernouk
你使用'UISearchController'吗?如果是使用'searchResultController.isActive'来检查搜索状态而不是自定义布尔变量。 – vadian
所以我需要声明searchResultController作为一个变量?只是将其换为searchResultController.isActive会导致未解决的标识符错误。我已经让searchController = UISearchController(searchResultsController:nil) – infernouk
感谢这个,但是如果我设置过滤,也= [运动]()怎么了我的意思将它映射到单元名称时,它不再是一个字符串?看到我的代码部分cell.textLabel?.text = filtered [indexPath.row] as String – infernouk
@infernouk检查编辑答案:) –
我试过这个,它不喜欢它,我是否也需要编辑变量或者只是插入规定的代码?错误无法将类型'String'的值转换为结束结果类型'Exercise'并在表格单元格名称语句中:无法将强制类型'Exercise'的值转换为键入'String' – infernouk