gpt4 book ai didi

ios - ReloadData() 不刷新 TableView

转载 作者:行者123 更新时间:2023-11-28 09:35:30 24 4
gpt4 key购买 nike

我有一个文本字段,当我输入一个词,然后按下一个按钮时,应该将这个词添加到表格 View 中。我知道数组正在更新,因为在按下按钮后,数组及其新值在控制台中打印正常。我在几个地方试过 reloadData() 但它什么也没做。这是我的代码:

import UIKit
class Arraytest: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var textField: UITextField?
var names = ["Jack", "Andy", "Guy", "Bruce", "Nick", "James", "Dominick"]
@IBAction func addButton(_ sender: Any) {
let name : String = textField!.text!
names.append(name)
textField?.text! = ""
for guy in names{
**print(guy)**
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = names[indexPath.row]
***tableView.reloadData()***
return cell
}
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = names[indexPath.row]
return cell
}
}

假设我在文本字段中键入 John,这是控制台打印的内容:

Jack
Andy
Guy
Bruce
Nick
James
Dominick
John

我知道数组工作正常,但不知道为什么当每个人都声称 reloadData() 工作时 tableView 不会重新加载(我确定它会工作,我只是犯了一个容易犯的错误!)..有什么想法吗?

编辑:好的,事实证明您必须从 TableView 中拖动 IBOutlet。我之前没有这样做的原因是因为我看了一个视频,他的工作没有建立联系。

最佳答案

您应该了解有关 TableView 及其工作原理的更多信息。请参阅 Apple 文档 Creating and Configuring a Table View

这里的方法 cellForRowAt 是 TableView 的数据源,当需要填充单元格数据时,它会自动从 TableView 中触发。您无法手动调用此方法。在 @IBAction func addButton() 中实现这个方法什么都不做。函数总是需要从另一个方法调用。因此,您应该删除 @IBAction func addButton() 中的 cellForRowAt

解决方案:从 Storyboard 中获取表格 View 导出。如果需要帮助,请参阅此处 Connect the UI to Code

@IBOutlet weak var tableView: UITableView?

然后在viewDidLoad()中设置tableview数据源和delegate

override func viewDidLoad() {
super.viewDidLoad()
tableView?.dataSource = self;
tableView?.delegate = self;
}

最后更新 @IBAction func addButton() 如下

@IBAction func addButton(_ sender: Any) {
let name : String = textField!.text!
names.append(name)
textField?.text! = ""
for guy in names{
print(guy)
}

tableView?.reloadData()
}

完整的源代码可能是这样的:

import UIKit
class Arraytest: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var textField: UITextField?
@IBOutlet weak var tableView: UITableView?

var names = ["Jack", "Andy", "Guy", "Bruce", "Nick", "James", "Dominick"]

override func viewDidLoad() {
super.viewDidLoad()
tableView?.dataSource = self;
tableView?.delegate = self;
}

@IBAction func addButton(_ sender: Any) {
let name : String = textField!.text!
names.append(name)
textField?.text! = ""
for guy in names{
**print(guy)**
}

tableView?.reloadData()
}

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = names[indexPath.row]
return cell
}
}

关于ios - ReloadData() 不刷新 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com