【问题标题】:TableView not display the array datasTableView 不显示数组数据
【发布时间】:2019-09-03 12:22:51
【问题描述】:
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var myTableView: UITableView!{
        didSet {
            tableView.dataSource = self
            //tableView.reloadData()
        }
    }

    var data = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        data = ["data1","data2", "data3"]
        //tableView.reloadData()
    }

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

        cell.lbl?.text = data[indexPath.row]

        return cell
    }
}

【问题讨论】:

  • 您发布的代码甚至无法编译...这是您的问题吗?或者,您没有发布您正在使用的实际代码吗?
  • 我只添加了我尝试一些解决方案的代码的重要部分。当然我还有其他必要的 tableview 功能。
  • 好的 - 好吧,“代码的重要部分” 错了!。您需要发布您的实际代码,否则人们只是在猜测您可能做错了什么。
  • @DonMag 我试图添加,但不知何故它给出了错误,我无法编辑,所以这是紧急情况,我只是这样发送
  • 您是否将您的单元格设计为 Storyboard 中的原型?如果是这样,您是否为此写了class?如果是这样,那个类的名称是什么?

标签: ios arrays swift uitableview


【解决方案1】:

您需要设置委托,但您没有正确使用代码。

@IBOutlet var myTableView: UITableView!
var data:[String]!{
    didSet {
        tableView.reloadData()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    tableView.dataSource = self
    tableView.delegate = self
    data = ["data1","data2", "data3"]

}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell

    cell.lbl?.text = data[indexPath.row]

    return cell
}
}

注意:如果您的单元格是 nib 文件,则在设置 delegate=self 之前在 viewDidLoad 中注册您的单元格

tableView.register(UINib(nibName: "nibname", bundle: nil), forCellReuseIdentifier: "myCell")

【讨论】:

  • 您的单元格是 nib 文件?
  • 是的,我使用 tableViewCell 类
  • 它给出了一个错误。 “原因:'Could not load NIB in bundle:'NSBundle
  • 你有 .swift 文件还是 .XIB 文件?
【解决方案2】:

为了让你开始,这就是你需要的所有代码:

class MyCell: UITableViewCell {
    @IBOutlet var lbl: UILabel!
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var myTableView: UITableView!

    var data = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        data = ["data1","data2", "data3"]

        // set the table view's dataSource
        myTableView.dataSource = self

    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell

        cell.lbl?.text = data[indexPath.row]

        return cell

    }

}

这假设你

  • 在 Storyboard 中创建了单元原型
  • 将其自定义类分配给MyCell
  • 给它一个标识符“myCell”
  • 为其添加标签并将其连接到@IBOutlet var lbl: UILabel!

【讨论】:

  • 是的,它是有效的,我认为问题在于我像你一样改变了课程,它有效。谢谢!!
【解决方案3】:

移动

tableView.dataSource = self

到 ViewDidLoad() 并添加

tableView.delegate = self

以下数据源

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

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多