ios - TableView 不重新加载数据

标签 ios swift tableview

我只是尝试使用从数组中提取的新项目重新加载 TableView,而 tableView 不会重新加载新项目。这是代码。有什么帮助吗?
导入 UIKit

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

        @IBOutlet weak var myTableView: UITableView!

        @IBAction func reloadData(sender: UIButton) {
            tableData = newItems
            self.tableViewController.tableView.reloadData()
        }

        var items = ["AAA", "BBB"]
        var newItems = ["XXX", "YYY"]

        var tableData = [String]()

        var tableViewController = UITableViewController(style: .Plain)

        override func viewDidLoad() {
            super.viewDidLoad()

            tableData = items
            var tableView = tableViewController.tableView
            tableView.dataSource = self
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }


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

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as UITableViewCell
            cell.textLabel?.text = self.tableData[indexPath.row]
            return cell
        }
    }

最佳答案

您的代码中有很多错误,这是您完整的工作代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var myTableView: UITableView!

    var items = ["AAA", "BBB"]
    var newItems = ["XXX", "YYY"]

    var tableData = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableData = items
        myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

        myTableView.delegate = self
        myTableView.dataSource = self


    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        return tableData.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        cell.textLabel?.text = tableData[indexPath.row]

        return cell
    }

    @IBAction func reloadData(sender: AnyObject) {

        tableData = newItems
        self.myTableView.reloadData()
    }
}

将你的代码与这段代码进行比较,你就会明白你做错了什么。

HERE是更多信息的示例项目。

关于ios - TableView 不重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537123/

相关文章:

ios - 如何为 UIPageViewController Delegate 创建自定义类?

swift - 从一点 Swift 记录 Controller 方法

ios - UITableViewCell 没有可见的@interface 声明选择器 didReceiveMemoryWarning

ios - 旋转设备时调整 tableView 的大小

javafx - 如何确定记录是否存在于 javafx 的 TableView 中

ios - 标签文本有多种字体、样式和颜色

ios - React Native - React Native 可以做什么,不可以做什么?

ios - 是否可以使用 iOS 6 实现 3D map View

ios - 使用 Swift 的 AWS DynamoDB 查询

ios - 用于 iOS 开发的组件和 UI 套件?