///我只想过滤当用户在文本字段中输入文本时在集合视图单元格中显示的数组列表。 在文本字段编辑中,将根据文本字段输入对收集的数据进行排序和过滤。如果集合视图具有关于状态的数据,则如果我在文本字段中输入“ A”,则所有集合数据将进行排序并显示所有以字母“ A”开头的状态名称。请告诉我这样做的逻辑。
ios iphone
这是我的项目的链接---- 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!
}
答案 0 :(得分:1)
在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
的目标添加到.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 < / strong> d”,“ sk 已内”,“ th 已内有皮肤”, “ tw in in”,“ upw in d”,“ absc in d”,“ resc in d ”,“ downw in d”,“逆风”, “ tailw in d”,“ whirlw in d”]
答案 1 :(得分:0)
每次dataSource
textField's
发生变化时,您都需要更新text
。
您可以使用textField's text
方法-UITextFieldDelegate
textField(_:shouldChangeCharactersIn:replacementString:)
中的更改
我采取了2个数组:
1。 arr
:它将包含您可以在collectionView
2。。dataSourceArr
:它将包含基于filtered content
的{{1}}中的arr
。最初,两个textField's text
将具有相同的内容。
arrays
答案 2 :(得分:0)
您不应该提出这样的问题,还应该提供您尝试过的代码
let array = ["Alabama", "Alaska", "Arizona", "Arkansas", "Aalborg", "Indiana", "New Jersey"]
let searchtext = "A"
let filterArray = array.filter({ $0.hasPrefix(searchtext) }).sorted()