我是编码和核心数据的新手。我有一个名为";游戏";其具有2个属性";标题";(字符串(和";保存";(Bool(。游戏是实体"游戏"的父类别;玩家";具有一个属性";playerName";(字符串(。我希望该应用程序为所有具有游戏标题的玩家更新标题和保存的属性";"新游戏";基于他们按下保存游戏按钮时的输入。我希望在不知道表视图中玩家的确切数量的情况下更新属性。
`导入UIKit导入CoreData
类ViewController:UIViewController{
@IBOutlet weak var playerNames: UITableView!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var playerArray = [Player]()
var savedGames = [Games]()
var selectedGameName : Games?
var newGameName = "Test"
let savedGame = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
playerNames.dataSource = self
let newGame1 = Games(context: context)
newGame1.title = "New Game"
newGame1.saved = false
savedGames.append(newGame1)
selectedGameName = newGame1
saveItems()
let newPlayer1 = Player(context: context)
newPlayer1.playerName = "Player 1"
newPlayer1.parentCategory = selectedGameName
newPlayer1.title = selectedGameName?.title!
newPlayer1.saved = selectedGameName!.saved
playerArray.append(newPlayer1)
saveItems()
loadPlayers()
self.playerNames.reloadData()
}
@IBAction func addButtonPressed(_ sender: UIButton) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New Player", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add Player", style: .default){ [self](action) in
//what happens once the user clicks the add player button
let newPlayer = Player(context: context)
newPlayer.playerName = textField.text!
newPlayer.parentCategory = selectedGameName
newPlayer.title = selectedGameName?.title!
newPlayer.saved = selectedGameName!.saved
playerArray.append(newPlayer)
saveItems()
self.playerNames.reloadData()
}
alert.addTextField { playerTextField in
playerTextField.placeholder = "Player Name"
textField = playerTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
@IBAction func savedGamePressed(_ sender: UIButton) {
var textField = UITextField()
let alert = UIAlertController(title: "Please Name This Game", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Save Game", style: .default){ [self](action) in
//what happens once the user clicks the saved game button
newGameName = textField.text!
let newGameName = selectedGameName
//How do I change all players in tableview to have Game name of newGameName and a saved of savedGame?
}
alert.addTextField { playerTextField in
playerTextField.placeholder = "Game Name"
textField = playerTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
func saveItems(){
do{
try context.save()
}catch{
print("Error saving")
}
playerNames.reloadData()
}
func loadPlayers(){
let request : NSFetchRequest<Player> = Player.fetchRequest()
do{
playerArray = try context.fetch(request)
} catch{
print("Error fetching")
}
playerNames.reloadData()
}
}
//MARK:-Tableview函数
//显示表视图上的数据扩展ViewController:UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return playerArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = playerArray[indexPath.row].playerName
return cell
}
}`
我真的不知道该在这里尝试什么。我尝试了几种附加数据的变体,但都不起作用。
您通过复制数据并试图使其保持最新,使事情变得更加复杂。我会从Player中删除两个重复的属性,而是通过游戏关系访问它们。例如
playerArray[indexPath.row].parentCategory.title
这样,如果更改Game
对象的title
属性,则表视图中的所有行都将正确更新