Thanks Claude. I solved my filtering with the other scopes but the thing that lingers is I can't delete all the characters in the searchBar unless it's on scope 0 and once I delete characters I can't switch to any scope unless it's scope 0.
OOPer says
Can you show all the definitions of cPlayerArr, allTextArr, fetchAllPlayers(), fetchForwards(), fetchDefense(), fetchGoalies() and all the dataSource methods?
They may not be implemented consistently.
var cPlayerArr = [CurrentPlayers]()
//filters players based on scope
var allTextArr = [CurrentPlayers]()
func fetchForwards() {
do {
let request = CurrentPlayers.fetchRequest() as NSFetchRequestCurrentPlayers
let forwards = NSPredicate(format: "position CONTAINS 'RW' OR position CONTAINS 'LW' OR position CONTAINS 'C'")
request.predicate = forwards
self.allTextArr = try context.fetch(request)
DispatchQueue.main.async {
self.collections.reloadData()
}
}
catch {
}
}
func fetchDefense() {
do {
let request = CurrentPlayers.fetchRequest() as NSFetchRequestCurrentPlayers
let defense = NSPredicate(format: "position CONTAINS 'D'")
request.predicate = defense
self.allTextArr = try context.fetch(request)
DispatchQueue.main.async {
self.collections.reloadData()
}
}
catch {
}
}
func fetchGoalies() {
do {
let request = CurrentPlayers.fetchRequest() as NSFetchRequestCurrentPlayers
let goalies = NSPredicate(format: "position CONTAINS 'G'")
request.predicate = goalies
self.allTextArr = try context.fetch(request)
DispatchQueue.main.async {
self.collections.reloadData()
}
}
catch {
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) - UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colcell", for: indexPath) as! PlayerColCell
if (searchBar.text != "" || searchBar.selectedScopeButtonIndex 0) {
item = allTextArr[indexPath.row]
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 0.5
cell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.6)
cell.update(with: item)
return cell
} else {
item = cPlayerArr[indexPath.row]
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 0.5
cell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.6)
cell.update(with: item)
return cell
}
}