ios - 如何过滤附件视图中的集合视图的数组列表
问题描述
// 我只想过滤当用户在文本字段中输入文本时显示在集合视图单元格中的数组列表。在文本字段编辑集合数据将根据文本字段输入进行排序和过滤。如果集合视图有关于状态的数据,如果我在文本字段中输入“A”,所有集合数据将被排序并显示以字母“A”开头的所有状态名称。请告诉我这个的逻辑。
苹果手机
这是我的项目的链接---- https://drive.google.com/drive/folders/1d56PWO2j6YcDU2AJyCseZC16dfUeV7Bd?usp=sharing //
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
if let customView = Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)?.first as? CustomView {
self.textField.inputAccessoryView = customView
}
}
}
class CustomView: UIView, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
let words = ["abscind","downwind","headwind","lind","rescind","sind","skinned","tailwind","thin-skinned","tinned","twinned","upwind","whirlwind","wind"]
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "cell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.words.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.label.text = self.words[indexPath.row]
return cell
}
}
class CustomCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
解决方案
filteredWords
在类中创建另一个数组CustomView
并在集合视图数据源方法中使用该数组
class CustomView: UIView, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
let words = ["abscind","downwind","headwind","lind","rescind","sind","skinned","tailwind","thin-skinned","tinned","twinned","upwind","whirlwind","wind"]
var filteredWords = [String]()
override func awakeFromNib() {
super.awakeFromNib()
filteredWords = words
self.collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "cell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.filteredWords.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.label.text = self.filteredWords[indexPath.row]
return cell
}
}
在ViewController
将目标添加到textField
for 中.editingChanged
。然后filter
,sort
数组又重新加载了collectionView
。
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
if let customView = Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)?.first as? CustomView {
self.textField.inputAccessoryView = customView
self.textField.addTarget(self, action: #selector(textChanged(_:)), for: .editingChanged)
}
}
@objc func textChanged(_ sender: UITextField) {
if let customView = textField.inputAccessoryView as? CustomView {
if textField.text!.isEmpty {
customView.filteredWords = customView.words
customView.collectionView.reloadData()
} else {
customView.filteredWords = customView.words
.filter({ $0.localizedCaseInsensitiveContains(textField.text!) }).sorted(by: {
if let range0 = $0.range(of: textField.text!, options: [.caseInsensitive], range: $0.startIndex..<$0.endIndex, locale: nil),
let range1 = $1.range(of: textField.text!, options: [.caseInsensitive], range: $1.startIndex..<$1.endIndex, locale: nil) {
return range0.lowerBound < range1.lowerBound
} else {
return false
}
})
customView.collectionView.reloadData()
}
}
}
}
排序后的数组将根据搜索文本的索引进行排序。前任。对于文本"in"
,过滤和排序的结果将是
[“l in d”, “s in d”, “t in ned”, “w in d”, “sk in ned”, “th in -skinned”, “tw in ned”, “upw in d”, "absc in d", "resc in d", "downw in d", "headwind", "tailw in d", "whirlw in d"]
推荐阅读
- arduino - 通过按下按钮打开和关闭机器人手的 arduino 代码出现问题
- android - WearOS Ticwatch 幻影 AGC
- mysql - MySQL 对列名区分大小写
- python - 尝试安装 python 3 模块时权限被拒绝
- sparql - 维基数据查询服务中的命名子查询慢
- pandas - 如何根据条件将月数据转换为 nan?
- mysql - 使用 Flutter 连接数据库时出现问题
- windows - host=$decideOnStartup 在 Windows EC2 上的 splunk 配置中不起作用
- entity-framework - 如果存在子视图,如何使用产品实体
- reactjs - React Admin dataProvider 在创建期间未发送 id