更新 UITableViewCell 中的 UILabel 框架
Update UILabel frame inside a UITableViewCell
我正在尝试更新包含待办事项标题的 uilabel 框架,以便在没有待办事项描述时它会垂直居中。
这里是cellForRowAt indexPath
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : customCell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! customCell
// Configure the cell...
let todo = todos[indexPath.row]
cell.setup(todo: todo.title, todoDescription : todo.todoDescription)
return cell
}
在自定义单元格中 class:
import UIKit
class customCell: UITableViewCell {
let titleLabel: UILabel! = UILabel(...) //cenvenience initializer setting the uilabel frame
let descriptionLabel: UILabel! = UILabel(...)
func setup(todo : String, todoDescription : String?) {
titleLabel.text = todo
if let todo_descr = todoDescription {
descriptionLabel.text = todo_descr
} else {
descriptionLabel.text = ""
titleLabel.frame = ... //update the frame of the todo title to be centered vertically in the cell, basically change the y of the frame
}
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.frame = ... //initial frame, which is not centered vertically
descriptionLabel.frame = ...
contentView.addSubview(titleLabel)
contentView.addSubview(descriptionLabel)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
当我打印 titleLabel
框架时,它会打印所需的框架属性。但是在模拟器中,frame还是layoutSubviews
.
中给label的那个
我已经在 setup
函数中尝试了 self.layoutIfNeeded()
,但它不起作用。如果存在或缺少待办事项描述,我如何更新单元格子视图?
在你调用的 cellForRowAtIndexPath 中
cell.setup(todo: todo.title, todoDescription : todo.todoDescription)
在此方法中,您将 titleLabel 的框架设置为垂直居中,并且一切正常。
但是在那个单元格调用方法 layoutSubviews() 之后,在这个方法中你设置标签框架初始,不依赖于描述文本。
titleLabel.frame = ... //initial frame, which is not centered vertically
descriptionLabel.frame = ...
所有标签都设置在它们的初始帧上。
问题是 layoutSubviews() 方法在 setup() 方法之后调用。
所以不需要在 layoutSubviews 方法中设置标签的框架。他们已经在标签初始值设定项或 setup() 方法中设置了正确的帧。
这种方式代码非常少,用Auto Layout Constraints
设计你的UITableViewCell
如下
- Todo 标题将是静态高度
- 在属性中将 Todo 描述的行数设置为 0。所以当没有描述 Todo 描述时会自动将其高度降低为零。因为我们在高度计算方法中添加了
UITableViewAutomaticDimension
然后在视图控制器中添加这两个数据源方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
我正在尝试更新包含待办事项标题的 uilabel 框架,以便在没有待办事项描述时它会垂直居中。
这里是cellForRowAt indexPath
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : customCell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! customCell
// Configure the cell...
let todo = todos[indexPath.row]
cell.setup(todo: todo.title, todoDescription : todo.todoDescription)
return cell
}
在自定义单元格中 class:
import UIKit
class customCell: UITableViewCell {
let titleLabel: UILabel! = UILabel(...) //cenvenience initializer setting the uilabel frame
let descriptionLabel: UILabel! = UILabel(...)
func setup(todo : String, todoDescription : String?) {
titleLabel.text = todo
if let todo_descr = todoDescription {
descriptionLabel.text = todo_descr
} else {
descriptionLabel.text = ""
titleLabel.frame = ... //update the frame of the todo title to be centered vertically in the cell, basically change the y of the frame
}
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.frame = ... //initial frame, which is not centered vertically
descriptionLabel.frame = ...
contentView.addSubview(titleLabel)
contentView.addSubview(descriptionLabel)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
当我打印 titleLabel
框架时,它会打印所需的框架属性。但是在模拟器中,frame还是layoutSubviews
.
我已经在 setup
函数中尝试了 self.layoutIfNeeded()
,但它不起作用。如果存在或缺少待办事项描述,我如何更新单元格子视图?
在你调用的 cellForRowAtIndexPath 中
cell.setup(todo: todo.title, todoDescription : todo.todoDescription)
在此方法中,您将 titleLabel 的框架设置为垂直居中,并且一切正常。 但是在那个单元格调用方法 layoutSubviews() 之后,在这个方法中你设置标签框架初始,不依赖于描述文本。
titleLabel.frame = ... //initial frame, which is not centered vertically
descriptionLabel.frame = ...
所有标签都设置在它们的初始帧上。 问题是 layoutSubviews() 方法在 setup() 方法之后调用。 所以不需要在 layoutSubviews 方法中设置标签的框架。他们已经在标签初始值设定项或 setup() 方法中设置了正确的帧。
这种方式代码非常少,用Auto Layout Constraints
UITableViewCell
如下
- Todo 标题将是静态高度
- 在属性中将 Todo 描述的行数设置为 0。所以当没有描述 Todo 描述时会自动将其高度降低为零。因为我们在高度计算方法中添加了
UITableViewAutomaticDimension
然后在视图控制器中添加这两个数据源方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}