【问题标题】:TableViewCell doesn't show imageViewTableViewCell 不显示 imageView
【发布时间】:2017-11-25 09:23:09
【问题描述】:

我将单元格添加到我的 tableView 中,我想在任何单元格中显示图像我向我的类“MathViewController”添加了一个图像数组,我想在每个单元格中显示这些图像。但在任何单元格中都没有显示图像。做有人知道我的问题吗?如果你帮助我,我对 swift 很陌生,那会很棒。 它是我的 MathTableViewCell 类,用于从情节提要中获取图像:

import UIKit
class MathTableViewCell: UITableViewCell{

    @IBOutlet weak var imageCell: UIImageView!
}

这里是 MathViewController 类:

import UIKit
class MathViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableController: UITableView!

    let items = ["Calculatorimg" , "ss"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableController.register(UITableViewCell.self, forCellReuseIdentifier: "MathTableViewCell")
        tableController.rowHeight = UITableViewAutomaticDimension
        tableController.estimatedRowHeight = 44
        tableController.delegate = self
        tableController.dataSource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "MathTableViewCell", for: indexPath)  as? MathTableViewCell{
            cell.imageCell.image = UIImage(named: items[indexPath.row])
            return cell

        }
        return UITableViewCell()
    }

}

这是我的故事板,因为您的单元格标识符已设置为 MathTableViewCell: storyboard

【问题讨论】:

  • 这些图像在资产或文件夹中。如果这些图像在文件夹中,那么您必须提供该图像的扩展名,例如“ss.png”
  • @jitendraModi 他们在资产中
  • 只打印 items[indexPath.row] 并检查它的输出。
  • @nino 你从情节提要创建了单元格吗?
  • 两个注意事项:1) 如果您使用没有额外笔尖的情节提要,永远不要注册单元格。 2) 移除cellForRow : let cell = tableView.dequeue ... indexPath) as! MathTableViewCell 中的可选绑定以排除设计错误。

标签: ios swift uitableview


【解决方案1】:

第 1 部分:如果为单元格创建新的 Xib。

整个代码是正确的,但是忘记将 UITableViewCell Nib 文件注册到 tableView 中。

ViewDidLoad中添加以下代码

tableController.register(UINib(nibName: "MathTableViewCell", bundle: nil), forCellReuseIdentifier: "MathTableViewCell")

第 2 部分:交叉验证 CellReuseIdentifier 是否正确。

【讨论】:

  • 你怎么知道OP使用了额外的笔尖?
  • 我做到了,我收到了一个错误:无法加载名为“MathTableViewCell”的捆绑包中的 NIB
  • 是你的 CellReuseIdentifier = MathTableViewCell 吗?
  • 完全没有理由使用额外的笔尖。
  • @vadian 如果为单元创建了新的 xib,则需要注册单元 nib 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 2017-07-17
  • 2020-09-07
相关资源
最近更新 更多