Thursday, October 8, 2026
HomeiOS Developmentios - How do I entry a UITextField in a xib from...

ios – How do I entry a UITextField in a xib from a tableview?

[ad_1]

I’m new to programing and Swift and attempting to construct a easy app that lets a consumer change a rating and save that rating to core information whereas additionally printing the rating. How do I accurately entry the gameCellScore textview inside the xib in order that I can save and print it? I attempted utilizing didSelectRowAt however nothing occurs once I click on on the textview and it crashes once I choose the row.

Core information has an entity referred to as Gamers and two attributes referred to as playerName and round1.

Viewcontroller code

@IBOutlet weak var gamePlayers: UITableView!


let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var playerArray = [Player]()

override func viewDidLoad() {
    tremendous.viewDidLoad()
    // Do any extra 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 = Participant(context: context)
    newPlayer1.playerName = "Participant 1"
    newPlayer1.round1 = 1
    playerArray.append(newPlayer1)
    
    let newPlayer2 = Participant(context: context)
    newPlayer2.playerName = "Participant 2"
    newPlayer2.round1 = 2
    playerArray.append(newPlayer2)
    
    saveItems()
    loadPlayers()
   
    self.gamePlayers.reloadData()
    
    
}

func saveItems(){
    do{
        strive context.save()
    }catch{
        print("Error saving")
    }
}

func loadPlayers(){
    let request : NSFetchRequest<Participant> = Participant.fetchRequest()
    do{
       playerArray = strive context.fetch(request)
    } catch{
        print("Error fetching")
    }
}

extension ViewController: UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
    return playerArray.depend
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "GameTableViewCell", for: indexPath) as! GameTableViewCell
    
    cell.gameCellLabel.textual content = playerArray[indexPath.row].playerName
    cell.gameCellScore.textual content = String(playerArray[indexPath.row].round1)
    
    return cell
    
}

extension ViewController: UITableViewDelegate{

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCell(withIdentifier: "GameTableViewCell", for: indexPath) as! GameTableViewCell
    let changePlayer = cell.gameCellScore.textual content
    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

class GameTableViewCell: UITableViewCell {

@IBOutlet weak var gameCellLabel: UILabel!
@IBOutlet weak var gameCellScore: UITextField!

override func awakeFromNib() {
    tremendous.awakeFromNib()
    // Initialization code
}

override func setSelected(_ chosen: Bool, animated: Bool) {
    tremendous.setSelected(chosen, animated: animated)

    // Configure the view for the chosen state
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments