gpt4 book ai didi

ios - 自定义单元格不使用 Storyboard在 UITabelView 中显示数据

转载 作者:行者123 更新时间:2023-11-28 23:22:00 25 4
gpt4 key购买 nike

所以我正在尝试构建没有 Storyboard的应用程序。我制作了自定义 tableview 单元格类并尝试显示数组中的标题和图像。代码运行甚至创建编号与数组相同但单元格为空的单元格。如果有人可以看一下这个,那就太好了。谢谢

这是我的自定义单元格

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

这是 TableView 的 View Controller 代码

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()
}

希望一切都有意义

这里添加了我的单元类的所有代码

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
}

关于ios - 自定义单元格不使用 Storyboard在 UITabelView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59502106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com