I have a project where people can assign a word when creating a collection view item and can use that term they assigned to the collection view item to search for it in another view, such as: someone creates item and names it 'milk' and then uses the work groceries so they can search for it later.
I am using CoreData and am saving that word upon the collection view item creation with an attribute, this it how my code looks for assigning the text fields filled in where the collection view cell is created:
@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
}
The hashtag is the one that stores the term to search with. I then have a new view controller where the item can be searched for using the the hashtag string. It uses a textField and a UIButton to reloadView and display items according to if their hashtag attribute matches the textField text. This is how I implemented it:
@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.
}
That is all the code in the view controller for searching but when I search for the term nothing comes up. I am not sure if this is a problem with the search button not actually reloading the view to show the cells or if the search function in general doesn't work. Does anyone know how to get the correct items to display according to their hashtag attribute?