我有一个项目,人们可以在创建集合视图项时指定一个单词,并可以使用他们分配给集合视图项的术语在另一个视图中搜索它,例如:某人创建项目并为其命名&# 39;牛奶'然后使用工作杂货,以便他们以后可以搜索它。
我正在使用CoreData并在使用属性创建集合视图项时保存该单词,这就是我的代码查找分配填充在创建集合视图单元格的文本字段的方式:
@IBAction func doneTran(_ sender: UIButton) {
if textFieldT.text != "" {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let task = Task(context: context)
task.name = textFieldT.text!
task.desc = descBox.text!
task.hashtag = hashtagField.text!
task.pid = pidStore
(UIApplication.shared.delegate as! AppDelegate).saveContext()
// Saves info to CoreData
}
标签是存储要搜索的字词的标签。然后我有一个新的视图控制器,可以使用hashtag字符串搜索项目。它使用textField和UIButton来重新加载View并根据它们的hashtag属性是否与textField文本匹配来显示项目。这就是我实现它的方式:
@IBOutlet weak var resultsColl: UICollectionView!
@IBOutlet weak var textFieldFHash: UITextField!
var taskTwo : [Task] = []
var activeCell : HashCollectionViewCell!
var testHashAt = String()
override func viewDidLoad() {
super.viewDidLoad()
self.resultsColl.delegate = self
self.resultsColl.dataSource = self
testHashAt = textFieldFHash.text!
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
getData()
resultsColl.reloadData()
testHashAt = textFieldFHash.text!
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return taskTwo.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let taskTHash = taskTwo[indexPath.row]
let cellHash = collectionView.dequeueReusableCell(withReuseIdentifier: "nCell", for: indexPath) as! HashCollectionViewCell
if testHashAt == taskTHash.hashtag {
cellHash.labelFHash?.text = taskTHash.name
}
else {
print("blank cell?")
}
if cellHash.labelFHash.text == "" {
print("cell is nil")
}
cellHash.layer.cornerRadius = 25
return cellHash
}
@IBAction func searchHashBtn(_ sender: UIButton) {
getData()
resultsColl.reloadData()
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest()
fetchRequest.predicate = NSPredicate.init(format: "hashtag = %@", testHashAt);
taskTwo = try context.fetch(fetchRequest)
}
catch {
print("Get Data failed")
}
// Fetches data.
}
这是视图控制器中用于搜索的所有代码,但是当我搜索该术语时,没有任何内容出现。我不确定这是否是搜索按钮实际上没有重新加载视图以显示单元格或者搜索功能一般不起作用的问题。有谁知道如何根据他们的hashtag属性显示正确的项目?
答案 0 :(得分:2)
请尝试以下代码:
import UIKit
class TDSearchVC: UIViewController , UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate{
@IBOutlet var textSearch: UITextField!
@IBOutlet var tblSearchResult: UITableView!
var arrData : [String] = []
var arrFilterData : [String] = []
var isSearch : Bool!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
isSearch = false
arrData = ["Apple", "Banana", "Chikoo", "Brew", "Cherry", "Mango", "Lotus", "Peacock", "Temple", "Pine Apple","Glass", "Rose", "Church", "Computer", "Carrot"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK:- textfield
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
var searchText = textField.text! + string
if string == "" {
searchText = (searchText as String).substring(to: searchText.index(before: searchText.endIndex))
}
if searchText == "" {
isSearch = false
tblSearchResult.reloadData()
}
else{
getSearchArrayContains(searchText)
}
return true
}
// Predicate to filter data
func getSearchArrayContains(_ text : String) {
var predicate : NSPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", text)
arrFilterData = (arrData as NSArray).filtered(using: predicate) as! [String]
isSearch = true
tblSearchResult.reloadData()
}
// MARK:- TableView Delegates
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if isSearch! {
return arrFilterData.count
}
else{
return arrData.count
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
if isSearch! {
cell.textLabel?.text = arrFilterData[indexPath.row]
}
else{
cell.textLabel?.text = arrData[indexPath.row]
}
return cell
}
}
答案 1 :(得分:0)
如果要使用TextField在tableView中搜索任何单词。您可以使用
func textFieldDidChangeSelection(_ textField:UITextField)