Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios ReloadData()不刷新TableView_Ios_Arrays_Swift_Uitableview - Fatal编程技术网

Ios ReloadData()不刷新TableView

Ios ReloadData()不刷新TableView,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,我有一个文本字段,当我输入一个单词,然后按下一个按钮时,应该将该单词添加到tableview中。我知道数组正在更新,因为按下按钮后,带有新值的数组会在控制台中很好地打印出来。我在几个地方尝试过重新加载数据,但都没有效果。这是我的密码: import UIKit class Arraytest: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var textField: UITex

我有一个文本字段,当我输入一个单词,然后按下一个按钮时,应该将该单词添加到tableview中。我知道数组正在更新,因为按下按钮后,带有新值的数组会在控制台中很好地打印出来。我在几个地方尝试过重新加载数据,但都没有效果。这是我的密码:

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
我知道阵列工作正常,但这不是为什么当每个人都声称重新加载数据工作时tableView不会重新加载我相信它会这样做,我只是犯了一个简单的错误!。。有什么想法吗


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

您可以通过在名称数组中添加属性观察者来实现这一点。如果值有任何更改,将运行didset方法

  class Arraytest: UIViewController, UITableViewDelegate UITableViewDatasource {
     var names = ["Jack", "Andy", "Guy", "Bruce", "Nick", "James", "Dominick"] {
        didSet {
            self.tableView.reloadData();
        }
      }
   }
addButton函数中的tableview函数未运行。。将其移除并保持如下状态:

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

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

您必须将tableview func置于@iAction func addButton之外,因为它是uitableviewdatasource的一部分,是显示单元格所必需的。在此之后,您只需将tableView.reloadData放在names{…}循环中的for guy之后。

您必须从中删除tableView委托方法

@IBAction func addButton(_ sender: Any) {
}
方法&把它放在它之外。像这样创建此tableView表单情节提要的属性

@IBOutlet weak var tableView: UITableView!
在viewDidload方法中,像这样设置datasource和它的委托

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.datasource = self
    self.tableView.delegate = self
 }

希望有帮助。

假设tableview代理正确连接到viewcontroller,则代码有两个非常大的错误

首先,您的表视图缺少一个IBOulet引用。创建一个,您可以在该出口中调用reloadData方法

其次,您不应该在IBAction内调用公共func tableView\uTableView:UITableView,cellForRowAt indexath:indexPath->UITableViewCell委托方法。它是一个委托方法,它已经在侦听将触发其逻辑的事件或命令,在本例中是重载数据

因此,iAction应该只从textfield中获取值并将其添加到数组中,以最终调用reloadData,这将调用public func tableView\uu tableView:UITableView,cellForRowAt indexPath:indexPath->UITableViewCell

这个代码可以工作。。。希望有帮助:

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableview: UITableView!
    @IBOutlet weak var textField: UITextField?

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

    // MARK: tableview delegate methods
    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
    }

    // MARK: ibactions

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

您应该进一步了解表视图及其工作原理。请参阅苹果的文档

这里的方法cellForRowAt是表视图的数据源,当需要填充单元格数据时,它会自动从表视图中触发。无法手动调用此方法。在@IBAction func addButton内实现此方法不会产生任何效果。函数总是需要从另一个方法调用。因此,您应该删除@IBAction func addButton中的cellForRowAt

解决方案:从情节提要获取表视图出口。如果需要帮助,请看这里

然后在viewDidLoad中设置tableview数据源和委托

最后更新@IBAction func addButton,如下所示

完整源代码可能如下所示:

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

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

将连接从拆分视图控制器拖到视图控制器并使用重载数据。

我尝试这样做时出现了以下错误:对成员“tableView\uuu:numberOfRowsInSection:”的引用不明确@PeteHornsby我以前从未尝试过对扩展做任何事情?你能告诉我怎么做吗?可能是重复的很有趣,因为我在Youtube上看了一段Swift Guy的视频,他连接了数据源和委托属性,但没有一个用于tableView的@IBOutlet!很高兴我能帮忙。快乐编码
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
    }
}