简体   繁体   English

UITableView不会填充数据

[英]UITableView does not populate with data

I am trying to make a simple UITableView with custom cells (including two labels and one image view) but I can't get the table to be populated with data. 我正在尝试使用自定义单元格(包括两个标签和一个图像视图)创建一个简单的UITableView,但我无法使表格填充数据。

I have created the following struct for my data: 我为我的数据创建了以下结构:

struct feed {
  var title: String
  var subtitle: String
  var image: String
}

and here I have defined some sample data: 在这里我定义了一些示例数据:

var myfeed = [feed(title: "Test Feed", subtitle: "Welcome to feedFeed", image: "https://www.demo.com/imnage1.png"), feed(title: "Number 2", subtitle: "Demo?", image: "https://www.demo.com/imnage2.png")]

I have created an UITableView in my Storyboard, have configured the Custom cell and am using the Cell identifier "LabelCell". 我在Storyboard中创建了一个UITableView,配置了Custom单元格并使用了Cell标识符“LabelCell”。 I have created a separate cocoaTouchclass file for the UITableViewCell class: 我为UITableViewCell类创建了一个单独的cocoaTouchclass文件:

import UIKit

class ehappyTableViewCell: UITableViewCell {

    @IBOutlet weak var headlineTitleLabel: UILabel! 
    @IBOutlet weak var headlineTextLabel: UILabel!
    @IBOutlet weak var headlineImageView: UIImageView!

    override func awakeFromNib() {
       super.awakeFromNib()
       func tableView(_ tableView: UITableViewCell, heightForRowAt indexPath: IndexPath) -> CGFloat
           { 
              return 100.0;//Choose your custom row height
            }
         // Initialization code
    }    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}

In my ViewController file I have the following code: 在我的ViewController文件中,我有以下代码:

    @IBOutlet weak var table: UITableView!    

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

    func tableView(_ tableViwq: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:ehappyTableViewCell = self.table.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! ehappyTableViewCell!

        let headline = myfeed[indexPath.row]
        cell.headlineTitleLabel?.text = headline.title
        cell.headlineTextLabel?.text = headline.subtitle
        let urlWithoutHTTP = headline.image
        let httpAddition = "https:"
        let addition = "\(httpAddition)\(urlWithoutHTTP)"

        let url = URL(string: addition)

        DispatchQueue.global().async {
           let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
            DispatchQueue.main.async {
                cell.headlineImageView.image = UIImage(data: data!)
            }
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140.0;//Choose your custom row height
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.table.delegate = self
        self.table.dataSource = self
    }

When I run the app the table will not populate with the sample data I have created. 当我运行应用程序时,表格不会填充我创建的示例数据。 Any suggestions on what I can do? 关于我能做什么的任何建议?

  • If your cell is an xib then you have to register the cell by writing the following code in the viewDidLoad 如果您的单元格是xib,必须通过在viewDidLoad中编写以下代码来注册单元格

    self.table.register(UINib(nibName: "ehappyTableViewCell", bundle: nil), forCellReuseIdentifier: "LabelCell")

  • If it is designed inside the viewcontroller in storyboard then no need to register 如果它是在storyboard中的viewcontroller内设计的,则无需注册

  • Check if delegate and dataSource are given 检查是否给出了delegate和dataSource

  • Try replacing the line with following line 尝试用以下行替换该行

    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell") as? ehappyTableViewCell

You've got an incorrect spelling for cellForRowAt method 你有一个不正确的cellForRowAt方法拼写

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

Correct method 正确的方法

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

}

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

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