iOS Uikit-如何以编程方式创建自定义UIVIEW
我正在尝试创建一个自定义uiview(cardView
),并在UITAITEVIEW中显示
class CardView: UIView {
public var cardTitle = UILabel()
public var cardValue = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .secondarySystemBackground
self.addSubview(cardTitle)
self.addSubview(cardValue)
layer.cornerRadius = 10
cardTitle.frame = CGRect(x: 0, y: 10, width: self.width, height: 25)
cardValue.frame = CGRect(x: 0, y: cardTitle.bottom + 1, width: self.width, height: 25)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
TableView如下:
class StatsTableViewCell: UITableViewCell {
static let identifier = "StatsID"
private var cellTitleLabel: UILabel = {
let label = UILabel()
return label
}()
private var homeView: CardView = {
let card = CardView()
card.cardTitle.text = "Home"
return card
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview()
}
override func layoutSubviews() {
super.layoutSubviews()
cellTitleLabel.frame = CGRect(x: 10, y: 10, width: contentView.width - 20, height: 20)
homeView.frame = CGRect(x: 10, y: cellTitleLabel.bottom + 10, width: (contentView.width - 40) / 3, height: contentView.height - 25)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func addSubview() {
contentView.addSubview(cellTitleLabel)
contentView.addSubview(homeView)
}
func configure(with viewModel: StatsTableviewViewModel) {
cellTitleLabel.text = viewModel.cellTitle
homeView.cardValue.text = viewModel.homeValue
}
}
在我的cellforrowat
上,我可以尝试像这样配置单元格:
extension WelcomeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
viewmodels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: StatsTableViewCell.identifier, for: indexPath) as? StatsTableViewCell else {
fatalError()
}
print(viewmodels)
cell.configure(with: viewmodels[indexPath.row])
return cell
}
}
问题在于,在statsviewController
中,它显示了Homeview
背景和celltitlelabel
,但未显示cardTitle
and Code>和cardValue
i 的屏幕截图m使用扩展名更容易访问宽度和高度。这就是为什么您没有看到.frame.size.height
I'm trying to create a custom UIView (CardView
) and display it in a UITableView
class CardView: UIView {
public var cardTitle = UILabel()
public var cardValue = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .secondarySystemBackground
self.addSubview(cardTitle)
self.addSubview(cardValue)
layer.cornerRadius = 10
cardTitle.frame = CGRect(x: 0, y: 10, width: self.width, height: 25)
cardValue.frame = CGRect(x: 0, y: cardTitle.bottom + 1, width: self.width, height: 25)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
The TableView looks like this:
class StatsTableViewCell: UITableViewCell {
static let identifier = "StatsID"
private var cellTitleLabel: UILabel = {
let label = UILabel()
return label
}()
private var homeView: CardView = {
let card = CardView()
card.cardTitle.text = "Home"
return card
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview()
}
override func layoutSubviews() {
super.layoutSubviews()
cellTitleLabel.frame = CGRect(x: 10, y: 10, width: contentView.width - 20, height: 20)
homeView.frame = CGRect(x: 10, y: cellTitleLabel.bottom + 10, width: (contentView.width - 40) / 3, height: contentView.height - 25)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func addSubview() {
contentView.addSubview(cellTitleLabel)
contentView.addSubview(homeView)
}
func configure(with viewModel: StatsTableviewViewModel) {
cellTitleLabel.text = viewModel.cellTitle
homeView.cardValue.text = viewModel.homeValue
}
}
On my cellForRowAt
im tryin to configuring the cell like this:
extension WelcomeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
viewmodels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: StatsTableViewCell.identifier, for: indexPath) as? StatsTableViewCell else {
fatalError()
}
print(viewmodels)
cell.configure(with: viewmodels[indexPath.row])
return cell
}
}
The problem is that in the StatsViewController
it shows the homeView
background and the cellTitleLabel
but is not showing the cardTitle
and cardValue
Screenshot of the StatsTableView
I'm using an extension to access easier to width and height. That's why you're not seeing .frame.size.height
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论