简体   繁体   English

如何使用 Swift 中的按钮从数组更新 tableView 中的单元格?

[英]How to update cell in a tableView from array by using button in Swift?

So, I was trying to make Birthday App which notes name and birthday of the written textField values.因此,我试图制作 Birthday App,其中记录了书面文本字段值的名称和生日。 When i press button, those values are saved in two separated Arrays. Those names will be seen on the tableView.当我按下按钮时,这些值保存在两个分开的 Arrays 中。这些名称将在 tableView 上看到。 I can't see nameArray values on cells but the program builds successfully.我在单元格上看不到 nameArray 值,但程序构建成功。

My question is how can update tableView cell names with the values of my nameArray?我的问题是如何使用我的 nameArray 的值更新 tableView 单元格名称?

Thanks in advance提前致谢

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    
    @IBOutlet weak var nameTF: UITextField!
    @IBOutlet weak var birthdayTF: UITextField!
    @IBOutlet weak var firstTV: UITableView!
    
    var nameArray = [String]()
    var birthdayArray = [String]()
    
    let rowName = UITableViewCell()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        firstTV.delegate = self
        firstTV.dataSource = self
    }

    @IBAction func saveButton(_ sender: Any) {
       
        nameArray.append(nameTF.text!)
        birthdayArray.append(birthdayTF.text!)
        
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        rowName.textLabel?.text = nameArray[indexPath.row]
        return rowName
    }
}

You need to go through a few tutorials on using table views and reusable table view cells.您需要 go 通过一些有关使用表视图和可重用表视图单元格的教程。

I'm going to assume that, in Storyboard, you added a cell to the table view and set its Style to Basic.我将假设,在 Storyboard 中,您向表视图添加了一个单元格并将其样式设置为基本。 You need to give that prototype cell an Identifier -- such as "cell":您需要为该原型单元格提供一个标识符——例如“单元格”:

在此处输入图像描述

Then, in your cellForRowAt function, dequeue a reusable instance of that cell.然后,在您的cellForRowAt function 中,使该单元格的可重用实例出列。

Here's your modified code:这是您修改后的代码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    
    @IBOutlet weak var nameTF: UITextField!
    @IBOutlet weak var birthdayTF: UITextField!
    @IBOutlet weak var firstTV: UITableView!
    
    var nameArray = [String]()
    var birthdayArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        firstTV.delegate = self
        firstTV.dataSource = self
    }
    
    @IBAction func saveButton(_ sender: Any) {
        
        nameArray.append(nameTF.text!)
        birthdayArray.append(birthdayTF.text!)
        
        firstTV.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = nameArray[indexPath.row]
        return cell
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM