【发布时间】: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