Oh I forgot to mention that my data source is a set so it is not like that. I'm not sure how to get the value from the cell like you have done.
var favSet: Set<CurrentPlayers> {
FavouriteManager.shared.favSet
}
Post
Replies
Boosts
Views
Activity
I get the error '-[LearnHockey.FavCell CamButtonA:]: unrecognized selector sent to instance 0x7f9fe6038800'. I created the segue too but I don't know what to put in my willrowat exactly.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "hs" {
let destinationController = segue.destination as? HighlightsVC
destinationController!.playerName = GlobalFavPlayer.globalFav!.yahooName!
}
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
//not sure what to put in here
}
This is the best I could do with making a global variable but what do I put inside exactly willSelectRowAt, You can't put any segues in there.
struct GlobalFavPlayer {
func addObserver() {
NotificationCenter.default.post(name: .passFavNotification,
object: GlobalFavPlayer.globalFav)
}
static var globalFav: CurrentPlayers?
}
What exactly should I put in my button in my cell that does the action?
@IBAction func CamButtonA(_ sender: UIButton) {
//?
}
I am still confused I don't understand what you mean by cell reference. I am also not sure how to reference the VC inside of UITableViewController.
The thing is I don't want the whole cell to be selected. I just want the button to be.
One of the ways I'm trying to remove duplicates by turning it into a set then back into an array when called but that does not seem to work. I could use some more help.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int {
let noDArr = Array(Set(filteredArr))
if (searchBar.text != "" || searchBar.selectedScopeButtonIndex 0) {
return noDArr.count
} else {
return cPlayerArr.count
}
}
I will close the thread because we got nowhere.
Okay I fixed some of my issues, the only issue I now have is the one where multiple cells of the same player are shown when you type in the searchBar. I think it has to do with the updateFilteredArray method below. If we get a specific player we need to basically erase any duplicates. This only happens when selectedScope 0.
func updateFilteredArray(searchText: String) {
let filterArray: [CurrentPlayers]
if searchBar.selectedScopeButtonIndex 0 {
filterArray = filteredArr
} else {
filterArray = cPlayerArr
}
filteredArr = filterArray.filter { player - Bool in
if searchText.isEmpty {
return true
} else {
print(filteredArray.count)
return
player.yahooName!.lowercased().contains(searchText.lowercased())
}
}
}
OOper says
You may be doing something strange beyond my guess where you have not shown yet.
Okay I will look at my code again, I will recopy it.
Claude31 says
Isn't it let defense = NSPredicate(format: "position CONTAINS [D]")
I don't think so, I think it's "position CONTAINS 'D'".
OOPer says
I cannot reproduce the issue you have described. You may be doing something wrong somewhere.
I copied it exactly how it was written and it does fix a few things while giving me a few more issues.
Claude31 says
What happens when you delete all char in search bar ?
Can you delete all but the last ?
One difference I see, is that in case 0 you clear the allTextArr array (what a bizarre name for [CurrentPlayers] array) and you reload them in other cases.
Could you show the code of:
fetchAllPlayers()
I list my current issues with OOper's code and I called it allTextArr because I use text to filter but I changed it to filteredArr so it's more clear. Here is the code for fetchAllPlayers()
func fetchAllPlayers() {
parseJSON {
self.collections.reloadData()
}
}
OOper says
Thanks for showing your code.
Checking your dataSource methods, your UICollectionView (collections) shows allTextArr when searchBar.selectedScopeButtonIndex 0.
But in your searchBar(:textDidChange:), you update allTextArr from cPlayerArr.
Generally:
You need to re-fetch when selectedScopeButtonIndex is changed
You need to re-filter when searchText is changed
But your current searchBar(:textDidChange:) mixes up two different things -- re-fetch and re-filter.
I would write them as follows:
Okay I tried your solution but there are issues with selectedScope 0,
When I filter for a specific player in selectedScope 0, I see multiple cells of the same player instead of one.
When I delete all character it should clear and reload one of those fetch methods instead of giving multiple cells of the same player.
After I delete all characters and when I try to start typing again in the searchBar I see no cells shown.
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
}
}
I was able to do it.
awal says
If this is really when you want to save(), I suggest reading up on the lifecycle of an app and using appropriate scene delegate methods if that’s the flow your app used.
Of course, iOS doesn’t ever really tell you if your app has been force-terminated by the user.
applicationWillTerminate seems like the appropriate method because it's called when the user terminates the app without switching it to the background.