Ios 为什么';我的表视图单元格类实例是否在Swift代码中运行其初始值设定项?
我有一个自定义的UITableViewCell子类,如下所示Ios 为什么';我的表视图单元格类实例是否在Swift代码中运行其初始值设定项?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个自定义的UITableViewCell子类,如下所示 class GroupSelectionTableViewCell: UITableViewCell { // MARK: - Properties var groupSelectionLabel: UILabel = { let label = UILabel() label.textColor = .white label.backgroundColor
class GroupSelectionTableViewCell: UITableViewCell {
// MARK: - Properties
var groupSelectionLabel: UILabel = {
let label = UILabel()
label.textColor = .white
label.backgroundColor = .clear
label.font = UIFont.systemFont(ofSize: 18)
return label
}()
var groupSelectionImageView: UIImageView = {
let iv = UIImageView()
iv.backgroundColor = .clear
iv.setHeight(height: 25)
iv.setWidth(width: 25)
iv.contentMode = .scaleAspectFill
return iv
}()
// MARK: - LifeCycle
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configureUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Helper Functions
private func configureUI() {
self.addSubview(groupSelectionLabel)
groupSelectionLabel.centerY(inView: self)
groupSelectionLabel.anchor(left: self.leftAnchor, paddingLeft: 12)
self.addSubview(groupSelectionImageView)
groupSelectionImageView.centerY(inView: self)
groupSelectionImageView.anchor(right: self.rightAnchor, paddingRight: 12)
self.backgroundColor = .black
self.selectionStyle = .none
}
}
以及下面显示的UITableView的自定义子类
class GroupSelectionView: UITableView {
// MARK: - Properties
private let cellID = "GroupSelectionTableViewCell"
override init(frame: CGRect, style: UITableView.Style) {
super.init(frame: frame, style: style)
backgroundColor = .red
setHeight(height: 450)
setWidth(width: 300)
register(GroupSelectionTableViewCell.self, forCellReuseIdentifier: cellID)
rowHeight = 60
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension GroupSelectionView: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
return
}
}
extension GroupSelectionView: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
6
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! GroupSelectionTableViewCell
cell.groupSelectionLabel.text = "None"
cell.groupSelectionImageView.image = UIImage(named: "Tick")
return cell
}
}
但是,当我将表视图实例作为子视图添加到UIView()时,表视图单元格类没有运行其初始值设定项。我是否正确使用了register方法?我尝试过实例化table view子类,并在table view cell子类的初始值设定项中放入一个print语句,但没有打印出来。我是否忘了在table view子类上设置一些属性?我是否需要使用寄存器的Nib版本?任何帮助都将不胜感激。忘记设置数据源和委托 与您的问题无关,但是
UITableViewCell.CellStyle
在子类实例方法中是冗余的CellStyle
顺便说一句,我会将UITableViewController子类化,而不是UITableViewCell.CellStyle应该作为设计工作。但是,cellForRow
被调用了吗?如果没有,可能是因为它缺少数据源,和/或因为它的大小不可见(它不能显示单元格,所以它不会调用cellForRowAt,这将调用dequeueCell,它将调用init(样式:identifier)),但我想通过执行view.addSubView(myTableView)来构建视图的弹出模式类型@LarmecellForRow
未被呼叫如果您有新问题,请单击按钮询问。如果此问题有助于提供上下文,请包含指向此问题的链接-