[英]How do I access a UITextField in a xib from a tableview?
I am new to programing and Swift and trying to build a simple app that lets a user change a score and save that score to core data while also printing the score.我是编程和 Swift 的新手,并尝试构建一个简单的应用程序,让用户更改分数并将该分数保存到核心数据,同时打印分数。 How do I correctly access the gameCellScore textview inside of the xib so that I can save and print it?
如何正确访问 xib 内的 gameCellScore textview 以便保存和打印? I tried using didSelectRowAt but nothing happens when I click on the textview and it crashes when I select the row.
我尝试使用 didSelectRowAt 但是当我点击 textview 并且当我 select 该行时它崩溃了。
Core data has an entity called Players and two attributes called playerName and round1.核心数据有一个名为 Players 的实体和两个名为 playerName 和 round1 的属性。
Viewcontroller code视图控制器代码
@IBOutlet weak var gamePlayers: UITableView!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var playerArray = [Player]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let nib = UINib(nibName: "GameTableViewCell", bundle: nil)
gamePlayers.register(nib, forCellReuseIdentifier: "GameTableViewCell")
gamePlayers.delegate = self
gamePlayers.dataSource = self
let newPlayer1 = Player(context: context)
newPlayer1.playerName = "Player 1"
newPlayer1.round1 = 1
playerArray.append(newPlayer1)
let newPlayer2 = Player(context: context)
newPlayer2.playerName = "Player 2"
newPlayer2.round1 = 2
playerArray.append(newPlayer2)
saveItems()
loadPlayers()
self.gamePlayers.reloadData()
}
func saveItems(){
do{
try context.save()
}catch{
print("Error saving")
}
}
func loadPlayers(){
let request : NSFetchRequest<Player> = Player.fetchRequest()
do{
playerArray = try context.fetch(request)
} catch{
print("Error fetching")
}
}
extension ViewController: UITableViewDataSource{扩展视图控制器: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: "GameTableViewCell", for: indexPath) as! GameTableViewCell
cell.gameCellLabel.text = playerArray[indexPath.row].playerName
cell.gameCellScore.text = String(playerArray[indexPath.row].round1)
return cell
}
extension ViewController: UITableViewDelegate{扩展视图控制器:UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "GameTableViewCell", for: indexPath) as! GameTableViewCell
let changePlayer = cell.gameCellScore.text
playerArray[indexPath.row].setValue(Any?(changePlayer!), forKey: "round1")
saveItems()
loadPlayers()
self.gamePlayers.reloadData()
print(playerArray[indexPath.row].setValue(Any?(changePlayer!), forKey: "round1"))
tableView.deselectRow(at: indexPath, animated: true)
}
xib code xib代码
class GameTableViewCell: UITableViewCell { class GameTableViewCell:UITableViewCell {
@IBOutlet weak var gameCellLabel: UILabel!
@IBOutlet weak var gameCellScore: UITextField!
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.