Ios 自定义单元格未在UITableView中显示数据未使用情节提要
所以我尝试构建没有故事板的应用程序。我创建了自定义的tableview单元格类,并尝试从数组中显示标题和图像。代码运行,甚至创建与数组编号相同但单元格为空的单元格。如果有人能看一下,那就太好了。谢谢 这是我的定制手机Ios 自定义单元格未在UITableView中显示数据未使用情节提要,ios,swift,uitableview,Ios,Swift,Uitableview,所以我尝试构建没有故事板的应用程序。我创建了自定义的tableview单元格类,并尝试从数组中显示标题和图像。代码运行,甚至创建与数组编号相同但单元格为空的单元格。如果有人能看一下,那就太好了。谢谢 这是我的定制手机 class CoffeeCell: UITableViewCell { let cellImage = UIImageView() let cellTitalBackground = UIView() let cellTital = UILabel() override fu
class CoffeeCell: UITableViewCell {
let cellImage = UIImageView()
let cellTitalBackground = UIView()
let cellTital = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = #colorLiteral(red: 0.9324248433, green: 0.9268818498, blue: 0.9366856217, alpha: 1)
self.layer.cornerRadius = 20
self.addSubview(cellImage)
self.addSubview(cellTitalBackground)
self.addSubview(cellTital)
setUpCellImage()
setUpCellTitalBackground()
setUpCellTital()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setUpCell(Coffee: Coffee) {
self.cellImage.image = UIImage(named: Coffee.image)
self.cellTital.text = Coffee.tital
下面是表视图的视图控制器代码
import UIKit
class CoffeeVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
let logoImage = UIImage(named: "DIYCoffeeLogoDark")
let logoImageView = UIImageView()
let pageHeader = UILabel()
let coffeeTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
coffeeTableView.dataSource = self
coffeeTableView.delegate = self
view.addSubview(pageHeader)
view.addSubview(coffeeTableView)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.coffees.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = coffeeTableView.dequeueReusableCell(withIdentifier: "CoffeeCell", for: indexPath) as? CoffeeCell {
let coffee = DataService.instance.getCoffee()[indexPath.row]
cell.setUpCell(Coffee: coffee)
print ("it works")
return cell
} else {
return CoffeeCell()
}
希望一切都有意义
这里添加了我的cell类的所有代码
import UIKit
class CoffeeCell: UITableViewCell {
let cellImage = UIImageView()
let cellTitalBackground = UIView()
let cellTital = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = #colorLiteral(red: 0.9324248433, green: 0.9268818498, blue: 0.9366856217, alpha: 1)
self.layer.cornerRadius = 20
self.addSubview(cellImage)
self.addSubview(cellTitalBackground)
self.addSubview(cellTital)
setUpCellImage()
setUpCellTitalBackground()
setUpCellTital()
layoutIfNeeded()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setUpCell(Coffee: Coffee) {
self.cellImage.image = UIImage(named: Coffee.image)
self.cellTital.text = Coffee.tital
}
func setUpCellImage() {
cellImage.contentMode = .scaleAspectFill
cellImage.translatesAutoresizingMaskIntoConstraints = false
cellImage.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
cellImage.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
cellImage.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
cellImage.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
cellImage.clipsToBounds = true
}
func setUpCellTitalBackground() {
cellTitalBackground.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.7847549229)
cellTitalBackground.translatesAutoresizingMaskIntoConstraints = false
cellTitalBackground.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
cellTitalBackground.trailingAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
cellTitalBackground.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
cellTitalBackground.heightAnchor.constraint(equalToConstant: 40).isActive = true
cellTitalBackground.layer.cornerRadius = 20
cellTitalBackground.layer.maskedCorners = .layerMaxXMinYCorner
}
func setUpCellTital() {
cellTital.textColor = .black
cellTital.textAlignment = .center
cellTital.font = UIFont(name: "Futura", size: 30)
cellTital.adjustsFontSizeToFitWidth = true
cellTital.translatesAutoresizingMaskIntoConstraints = false
cellTital.leadingAnchor.constraint(equalTo: cellTitalBackground.leadingAnchor).isActive = true
cellTital.bottomAnchor.constraint(equalTo: cellTitalBackground.bottomAnchor).isActive = true
cellTital.trailingAnchor.constraint(equalTo: cellTitalBackground.trailingAnchor).isActive = true
cellTital.topAnchor.constraint(equalTo: cellTitalBackground.topAnchor).isActive = true
}
}我相信你已经把合同安排好了。您还需要如下设置tableViewCells的高度:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50 // whatever you want
}
请按如下方式更新您的代码。指定图像和标题时,图像和标题标签框似乎没有更新。所以,移动约束并将子视图添加到setUpCell方法
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = #colorLiteral(red: 0.9324248433, green: 0.9268818498, blue: 0.9366856217, alpha: 1)
self.layer.cornerRadius = 20
}
func setUpCell(Coffee: Coffee) {
self.addSubview(cellImage)
self.addSubview(cellTitalBackground)
self.addSubview(cellTital)
setUpCellImage()
setUpCellTitalBackground()
setUpCellTital()
self.cellImage.image = UIImage(named: Coffee.image)
self.cellTital.text = Coffee.title
}
输出:-
您必须检查为图像和标签正确设置的约束,并查看结果。当UITableViewCell实例为空时,您没有创建它
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = coffeeTableView.dequeueReusableCell(withIdentifier: "CoffeeCell", for: indexPath) as? CoffeeCell
if cell == nil {
cell = CoffeeCell(style: .default, reuseIdentifier: "CoffeeCell")
}
cell?.setUpCell(Coffee: coffee)
return cell!
您可以使用标识符来重用单元,但对于开始,单元为nil,从未创建过,因此必须在单元为nil时创建
这是我的定制手机,它运行得很好
感谢您的投入和帮助 有另一个外观和原因在自定义单元格中不起作用,我重写了awakeFromNib()函数,但因为我没有使用故事板,所以我需要使用如下所示的init函数
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
抱歉,这是一个愚蠢的错误,再次感谢。谢谢。是的,我输入了一个150的单元格高度,单元格显示在正确的高度,但缺少图像和标签。是的,因为您没有注册单元格。在cellForRow方法中,每次都创建新的单元格。可能是因为您没有为图像和标签设置正确的约束。很抱歉,我在这里发布的代码中遗漏了这一点,但是的,我已经做到了。为了检查cellForRowAt()代码的哪一部分正在运行而输入的print语句也会打印到控制台。但是由于某些原因,不能显示图像或标签。您必须为图像和标签设置边框大小。请共享CoffeeCell类的完整代码。谢谢。是的,我刚刚用所有代码更新了帖子