使用自定义维度在初始化时布局单元格子视图
Layout cell subviews at initialization using custom dimensions
问题 - 简短版本
尽管将我的单元格高度配置为 112,但我的单元格以标准高度 44 实例化。这会弄乱我的子视图,除非我使用额外的资源并调用 cell.layoutSubviews
上下文:
我创建了一个名为 ResearchCell
的 UITableViewCell
子类,在 init
中,我执行以下操作:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print(NSStringFromCGRect(self.frame)) // prints out the standard 320*44 cell
manageViews()
}
我正在为我的手机使用 xib
:
我目前的设置
在 ViewController
中,我在 viewDidLoad
中调用以下内容:
researchTableView.dataSource = researchDataSource
researchTableView.delegate = self
researchTableView.register(ResearchCell.self, forCellReuseIdentifier: "ResearchCell")
我也调用如下:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 112.0
}
在 researchDataSource
中,我执行以下操作:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ResearchCell", for: indexPath) as! ResearchCell
cell.topLineLabel?.text = "Hola"
cell.layoutSubviews() //At the moment I have to call this to re-layout the views
return cell
}
问题
除非我调用 cell.layoutSubviews()
(见上面的评论),否则单元格会自动采用 320*44 的标准大小,并且我以编程方式在单元格中布置的所有视图都乱七八糟。单元格的高度应为 112,这是我在 xib
和 tableView.delegate
中指定的。但是,为什么没有进行到cell创建呢?
感谢任何指点 - 谢谢!
您正在注册 class 而不是您的自定义笔尖。更改以下内容:
researchTableView.register(ResearchCell.self, forCellReuseIdentifier: "ResearchCell")
收件人:
researchTableView.register(UINib(nibName: "ResearchCell", bundle: nil), forCellReuseIdentifier: "ResearchCell")
这将从您的包中加载 nib,而不是实例化 class。
问题 - 简短版本
尽管将我的单元格高度配置为 112,但我的单元格以标准高度 44 实例化。这会弄乱我的子视图,除非我使用额外的资源并调用 cell.layoutSubviews
上下文:
我创建了一个名为 ResearchCell
的 UITableViewCell
子类,在 init
中,我执行以下操作:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print(NSStringFromCGRect(self.frame)) // prints out the standard 320*44 cell
manageViews()
}
我正在为我的手机使用 xib
:
我目前的设置
在 ViewController
中,我在 viewDidLoad
中调用以下内容:
researchTableView.dataSource = researchDataSource
researchTableView.delegate = self
researchTableView.register(ResearchCell.self, forCellReuseIdentifier: "ResearchCell")
我也调用如下:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 112.0
}
在 researchDataSource
中,我执行以下操作:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ResearchCell", for: indexPath) as! ResearchCell
cell.topLineLabel?.text = "Hola"
cell.layoutSubviews() //At the moment I have to call this to re-layout the views
return cell
}
问题
除非我调用 cell.layoutSubviews()
(见上面的评论),否则单元格会自动采用 320*44 的标准大小,并且我以编程方式在单元格中布置的所有视图都乱七八糟。单元格的高度应为 112,这是我在 xib
和 tableView.delegate
中指定的。但是,为什么没有进行到cell创建呢?
感谢任何指点 - 谢谢!
您正在注册 class 而不是您的自定义笔尖。更改以下内容:
researchTableView.register(ResearchCell.self, forCellReuseIdentifier: "ResearchCell")
收件人:
researchTableView.register(UINib(nibName: "ResearchCell", bundle: nil), forCellReuseIdentifier: "ResearchCell")
这将从您的包中加载 nib,而不是实例化 class。