【问题标题】:How to update cell in a tableView from array by using button in Swift?如何使用 Swift 中的按钮从数组中更新 tableView 中的单元格?
【发布时间】:2020-09-21 19:53:46
【问题描述】:

所以,我试图制作一个生日应用程序,它会记录所写文本字段值的名称和生日。当我按下按钮时,这些值将保存在两个单独的数组中。这些名称将在 tableView 上看到。我在单元格上看不到 nameArray 值,但程序构建成功。

我的问题是如何用我的 nameArray 的值更新 tableView 单元格名称?

提前致谢

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
    }
}

【问题讨论】:

  • firstTV.reloadData() 将值附加到saveButton() func 中的数组之后。
  • 谢谢,它确实有效。当我按下按钮时,它确实保存了值,但第二次按下它会删除前一个。我怎样才能同时保留多个数据?
  • 你好,@cemogolog。欢迎来到stackoverflow.com,每当您遇到问题时,您应该在提交问题之前先搜索它以获得您的答案。这里是。几个答案详细解释了如何使用表格视图。 stackoverflow.com/questions/33234180/…

标签: ios arrays swift xcode tableview


【解决方案1】:

您需要阅读一些关于使用表格视图和可重用表格视图单元格的教程。

我将假设,在 Storyboard 中,您向表格视图添加了一个单元格并将其样式设置为基本。你需要给那个原型单元一个标识符——比如“单元”:

然后,在您的 cellForRowAt 函数中,将该单元格的可重用实例出列。

这是你修改后的代码:

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
    }
}

【讨论】:

  • 非常感谢您的快速回答和热心支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 2020-12-10
相关资源
最近更新 更多