首页 > 解决方案 > UITableView 不填充数据

问题描述

我正在尝试使用自定义单元格(包括两个标签和一个图像视图)制作一个简单的 UITableView,但我无法让表格填充数据。

我为我的数据创建了以下结构:

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

在这里我定义了一些示例数据:

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")]

我在 Storyboard 中创建了一个 UITableView,配置了自定义单元格并使用了单元格标识符“LabelCell”。我为 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
    }

}

在我的 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
    }

当我运行应用程序时,表格不会填充我创建的示例数据。关于我能做什么的任何建议?

标签: iosswiftuitableview

解决方案


  • 如果您的单元格是xib,那么您必须通过在viewDidLoad中编写以下代码来注册单元格

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

  • 如果它是在情节提要的视图控制器内部设计的,则无需注册

  • 检查是否给出了委托和数据源

  • 尝试用以下行替换该行

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


推荐阅读