I'm trying to filter my players using searchBar with scope in a collectionview using a switch statement and my case 0 works but the issue is my other cases I can type into the searchBar but I can't seem to filter (nothing shows up) nor can I delete all the characters when I type.
I have 2 arrays my cPlayerArr is my original array when data is recieved and my allTextArr is my filtered array when any kind of filtering is done.
Code Block func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { |
allTextArr = cPlayerArr.filter({ player -> Bool in |
switch searchBar.selectedScopeButtonIndex { |
case 0: |
if searchText.isEmpty { |
fetchAllPlayers() |
} else { |
return |
player.yahooName!.lowercased().contains(searchText.lowercased()) |
} |
case 1: |
if searchText.isEmpty { |
fetchForwards() |
} else { |
print(searchText) |
return player.yahooName!.lowercased().contains(searchText.lowercased()) && player.position == "C" && |
player.position == "RW" && player.position == "LW" |
} |
case 2: |
if searchText.isEmpty { |
fetchDefense() |
} else { |
return player.yahooName!.lowercased().contains(searchText.lowercased()) && player.position == "D" |
} |
case 3: |
if searchText.isEmpty { |
fetchGoalies() |
} else { |
return player.yahooName!.lowercased().contains(searchText.lowercased()) && player.position == "G" |
} |
default: |
return false |
} |
return true |
}) |
collections.reloadData() |
} |
Code Block func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange: Int) { |
if (searchBar.selectedScopeButtonIndex == 0) { |
cPlayerArr.removeAll() |
allTextArr.removeAll() |
fetchAllPlayers() |
} else if (searchBar.selectedScopeButtonIndex == 1) { |
allTextArr.removeAll() |
fetchForwards() |
} else if (searchBar.selectedScopeButtonIndex == 2) { |
allTextArr.removeAll() |
fetchDefense() |
} else { |
allTextArr.removeAll() |
fetchGoalies() |
} |
} |
Code Block func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
if (searchBar.text != "" || searchBar.selectedScopeButtonIndex > 0) { |
return allTextArr.count |
} else { |
return cPlayerArr.count |
} |
} |