首页 > 解决方案 > 如何过滤附件视图中的集合视图的数组列表

问题描述

// 我只想过滤当用户在文本字段中输入文本时显示在集合视图单元格中的数组列表。在文本字段编辑集合数据将根据文本字段输入进行排序和过滤。如果集合视图有关于状态的数据,如果我在文本字段中输入“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!
}

标签: iosswift

解决方案


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将目标添加到textFieldfor 中.editingChanged。然后filtersort数组又重新加载了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"]


推荐阅读