简体   繁体   English

TableView不会重新加载数据

[英]TableView does not reload data

I am simply trying reload a TableView with new items pulled from an array and the tableView does not reload the new items. 我只是尝试使用从数组中提取的新项目重新加载TableView,而tableView不会重新加载新项目。 Here is the code. 这是代码。 Any help please? 有什么帮助吗?
import UIKit 导入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
        }
    }

There are many mistakes in your code and here is your complete working code: 您的代码中有很多错误,这是您完整的工作代码:

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

Compare your code with this code and you will understand what you have done wrong. 将您的代码与此代码进行比较,您将了解自己做错了什么。

HERE is sample project for more info. 这里是示例项目,以获取更多信息。

import UIKit 导入UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { ViewController类:UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var myTableView: UITableView!
var flag=1
var items = ["AAA", "BBB"]
var newItems = ["XXX", "YYY"]

var tableData = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    if flag==1{
        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) {
    if flag==1{
        tableData = items
        flag=0
    }else
    {
        tableData=newItems
        flag=1
    }
    self.myTableView.reloadData()
}

} }

import UIKit
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView! //var flag=1 var items = ["AAA", "BBB"] var newItems = ["XXX", "YYY"] var tableData = String override func viewDidLoad() { super.viewDidLoad() // if flag==1{ // 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) { // if flag==1{ // tableData = items // flag=0 // }else // { // tableData=newItems // flag=1 // } self.myTableView.reloadData() }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM