I figured out how to save the state of the switch when the state of the switch is changed. Thanks for your help on getting me there.
class SettingsCustomCell: UITableViewCell {
//...
@IBAction func switchValueChanged(_ sender: UISwitch) {
state?.settingValue = sender.isOn
let defaults = UserDefaults.standard
defaults.set(quickAddSwitch.isOn, forKey: "quickAdd")
}
}
class Settings: UIViewController, UITableViewDelegate, UITableViewDataSource {
//...
override func viewDidLoad() {
super.viewDidLoad()
cellStates = [
SettingsState(settingLabel: "Quick Add", settingValue: (UserDefaults.standard.bool(forKey: "quickAdd")))
]
}
}
Post
Replies
Boosts
Views
Activity
OK so I rewrote some code,
class Settings: UIViewController, UITableViewDelegate, UITableViewDataSource {
var data = [Settings_Custom_Cell]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
data = [SettingsCell(settingLabel: "Quick Add", settingValue: <#UISwitch#> )]
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! Settings_Custom_Cell
let array = data[indexPath.row]
cell.textLabel?.text = array.settingLabel.text
cell.settingValue.isOn = array.settingValue.isOn
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.textLabel?.font = UIFont.systemFont(ofSize: 20)
return cell
}
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
I am not sure if I am on the right track but what do I put in the 'settingValue' value in
data = [SettingsCell(settingLabel: "Quick Add", settingValue: <#UISwitch#> )]
OK so I rewrote some code,
class Settings: UIViewController, UITableViewDelegate, UITableViewDataSource {
var data = [Settings_Custom_Cell]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
data = [SettingsCell(settingLabel: "Quick Add", settingValue: <#UISwitch#> )]
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! Settings_Custom_Cell
let array = data[indexPath.row]
cell.textLabel?.text = array.settingLabel.text
cell.settingValue.isOn = array.settingValue.isOn
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.textLabel?.font = UIFont.systemFont(ofSize: 20)
return cell
}
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
I am not sure if I am on the right track but what do I put in the 'settingValue' value in
data = [SettingsCell(settingLabel: "Quick Add", settingValue: <#UISwitch#> )]
Yes, I setup storyboard. When I run the app, the switch displays fine but when I leave the view controller and return to the view controller the state of the switch is back to off.
Yes, I looked, found some stuff on Stack Overflow but everything is confusing. Can you point me in the right direction of a good article please?
How do I use a server without having to pay? Is there any free ones? If not, how do I use keychain?
Okay how do I save it somewhere else? Is there a way to save it somewhere else other than a server?
Response is defined
import Foundation
struct Response: Codable {
let results: Player_Data
let status: String
}
struct Player_Data: Codable {
let playerID: String
let position: String
let displayName: String
let fname: String
let team: String
let byeWeek: String
let standDev: String
let nerdRank: String
let positionRank: String
let overallRank: String
}
The error is
failed to convert keyNotFound(CodingKeys(stringValue: "results", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"results\", intValue: nil) (\"results\").", underlyingError: nil))
It looks like the API is in a dictionary? Does that matter on how I format the Struct?
Here is the actual API
https://www.fantasyfootballnerd.com/service/draft-rankings/json/fmt8fvc8xg2s/1/
Yes, the leading whitespace had to be removed but now it is executing line 21 - 23. The error is 'The data couldn’t be read because it isn’t in the correct format.'
Thank you for your help.
1. I tried implementing didSelectRowAt function but how do I implement it that when a row is selected with the league name that it will segue to another view controller load the players for that selected league in a table view?3. Yes, table in destination VC. I setup a segue in storyboard to segue to another view controller to load myLeaguePlayersData.4. The destination VC class is MyLeague_2019class MyLeague_2019: UIViewController, UITableViewDelegate, UITableViewDataSource {
var players: [[PlayerData]] = [[]]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
players = Database_2019.shared.myLeaguePlayersData
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players[section].count
}
func numberOfSections(in tableView: UITableView) -> Int {
return players.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let players = Database_2019.shared.myLeaguePlayersData[indexPath.section][indexPath.row]
let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row+1). \(players.name), \(players.team) - \(players.position)"
cell.textLabel?.adjustsFontSizeToFitWidth = true
cell.textLabel?.font = UIFont.systemFont(ofSize: 22)
return cell
}
@IBOutlet weak var table: UITableView!
}5. Yes, I want to pass the whole myLeaguePlayersData to the destination.Yes, I do need to declare as an array of PlayerData.Yes, I do want to keep in memory that those players are added as a group.
var myLeaguePlayers is an array of players.var myLeaguePlayersData is an array of the added leagues players.myLeaguePlayers = [Aaron Rodgers, GB - QB, Alvin Kamara, NO - RB, Tyreek Hill, KC - WR]
myLeaguePlayersData = [[Patrick Mahomes, KC - QB, David Johnson, ARI - RB, Michael Thomas, NO - WR], [Matt Ryan,
ATL - QB, Joe Mixon, CIN - RB], [Aaron Rodgers, GB - QB, Alvin Kamara, NO - RB, Tyreek Hill, KC - WR]]myLeaguePlayers is the current players in the league. I want to append myLeaguePlayers to myLeaguePlayersData.I do have myLeaguePlayers and myLeaguePlayersData initialized. I didn't show it but here it is:init() {
myLeagues = [String]()
myleaguePlayers = [PlayerData]()
myLeaguePlayersData = [[PlayerData]]()
}
var myLeagues: [String] is the list of league names.var myLeaguePlayers: [PlayerData} is the players.var myLeaguePlayersData: [[PlayerData]] is an array of the players.For instance, I want to be able to append myLeaguesPlayers to myLeaguePlayersData when a league is added but I want the added myLeaguePlayers to be connected to the added myLeagues.The result I want is when the added leagues row is clicked, it will segue to another view controller and load the connected myLeaguePlayersData element inb a table.What is your sugestion on how to do this?
Thank you. It worked.I created another variable in databasefileprivate var allPlayersReset: [PlayerData]Then I initialized it by creating another data set.Then I created a function and set allPlayers = allPlayersReset.func resetDraft() {
allPlayers = allPlayersReset
}It works perfect.
Yes this worked. I had to add it in viewWillAppear though.