如何使用 Swift 中的按钮从数组中更新 tableView 中的单元格?
How to update cell in a tableView from array by using button in Swift?
所以,我试图制作生日应用程序,它记录了书面文本字段值的名称和生日。当我按下按钮时,这些值保存在两个单独的数组中。这些名称将显示在 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
}
}
您需要完成一些关于使用 table 视图和 可重用 table 视图单元格的教程.
我假设您在情节提要中向 table 视图添加了一个单元格并将其样式设置为基本。您需要为该原型单元格提供一个标识符——例如“cell”:
然后,在您的 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
}
}
所以,我试图制作生日应用程序,它记录了书面文本字段值的名称和生日。当我按下按钮时,这些值保存在两个单独的数组中。这些名称将显示在 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
}
}
您需要完成一些关于使用 table 视图和 可重用 table 视图单元格的教程.
我假设您在情节提要中向 table 视图添加了一个单元格并将其样式设置为基本。您需要为该原型单元格提供一个标识符——例如“cell”:
然后,在您的 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
}
}