I have a Swift class called Person that is created by the user and saved using Core Data. When clicking on a Person, the user can see more of the Person's information, and can navigate to their ListItems to select how that Person does on the user's ListItems.
The problem is that these selections aren't persistent, and also that I have a cell reuse issue. I've tried solving this with a ListItems Class that, for each individual listItem, it also has a listItemStatus. I've tried a dictionary setup with the same principle, which I couldn't get to work, but I think it would have applied Person 1's listItemStatuses to Person2,3,4 etc.
My question is: How do I go about achieving my goal? I'm new enough to programming that I haven't been able to figure it out after a few weeks of googling around and looking for relevant questions on Stack Overflow.
This is my Person class:
import Foundation
import CoreData
class Person:NSManagedObject {
@NSManaged var firstName:String
@NSManaged var lastName:String?
@NSManaged var score:String?
@NSManaged var age:String
@NSManaged var locationCity:String
@NSManaged var locationState:String
@NSManaged var phoneNumber:String?
@NSManaged var image:NSData?
@NSManaged var isVisited:NSNumber?
@NSManaged var notes:String?
}
This is my ListItems class:
import Foundation
class ListItems {
var listItem = ""
var listItemStatus = Int()
init(listItem:String, listItemStatus: Int) {
self.listItem = listItem
self.listItemStatus = listItemStatus
}
}
I have a View Controller, ListItemDetailViewController, that has a lot of structural code, but the main piece that matters is the listItems variable (eventually I'd like to populate this from a elsewhere, but for now it's explicitly declared using the ListItems class):
// 0 = x
// 1 = ?
// 2 = o
// 3 = check
var listItems:[ListItems] = [
ListItems(listItem: "Close with mom", listItemStatus: 1), ListItems(listItem: "Polite", listItemStatus: 1), ListItems(listItem: "Great teeth", listItemStatus: 1), ListItems(listItem: "Good with money", listItemStatus: 1), ListItems(listItem: "Clever", listItemStatus: 1), ListItems(listItem: "Treats service workers with respect", listItemStatus: 1), ListItems(listItem: "Good friends", listItemStatus: 1), ListItems(listItem: "Patient", listItemStatus: 1), ListItems(listItem: "Athletic", listItemStatus: 1), ListItems(listItem: "Apologizes", listItemStatus: 1)
]
Where the majority of the action happens is in the ListItemDetailTableViewCell (that's probably sloppy naming terminology, my apologies). I had originally thought I could make an IBOutlet to link up the listItemStatus of each listItem with its variable, but apparently Xcode doesn't let you do outlets with repeating content like a table view:
import UIKit
class ListItemDetailTableViewCell: UITableViewCell {
@IBOutlet var listItemLabel: UILabel!
@IBOutlet var checkButton:UIButton!
@IBOutlet var xButton:UIButton!
@IBOutlet var oButton:UIButton!
@IBOutlet var questionButton:UIButton!
var listItemStatus = 1
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// Ideally this toggles the button colors for the list items
@IBAction func toggleListStatusButton(sender: UIButton) {
// check button clicked
if sender == checkButton {
listItemStatus = 3
checkButton.backgroundColor = UIColor(red: 18.0/255.0, green: 255.0/255.0, blue: 0.0/255.0, alpha: 1.0)
oButton.backgroundColor = UIColor.clearColor()
xButton.backgroundColor = UIColor.clearColor()
questionButton.backgroundColor = UIColor.clearColor()
} else if sender == oButton {
listItemStatus = 2
checkButton.backgroundColor = UIColor.clearColor()
oButton.backgroundColor = UIColor(red: 106.0/255.0, green: 221.0/255.0, blue: 255.0/255.0, alpha: 1.0)
xButton.backgroundColor = UIColor.clearColor()
questionButton.backgroundColor = UIColor.clearColor()
} else if sender == xButton {
listItemStatus = 0
checkButton.backgroundColor = UIColor.clearColor()
oButton.backgroundColor = UIColor.clearColor()
xButton.backgroundColor = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
questionButton.backgroundColor = UIColor.clearColor()
} else if sender == questionButton {
listItemStatus = 1
checkButton.backgroundColor = UIColor.clearColor()
oButton.backgroundColor = UIColor.clearColor()
xButton.backgroundColor = UIColor.clearColor()
questionButton.backgroundColor = UIColor.clearColor()
}
}
}
Ideally, I'd be able to update the ListItems class in such a way that the ListItemStatus for each ListItem is saved for each individual Person in Core Data. I'm not even sure I'm using the right tool here, maybe a dictionary would be better than a dedicated class?