todoのタイトルが含まれているuilabelのフレームを更新しようとしています。そのため、todoの説明がないときに垂直に中央揃えされます。ここ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
}
カスタムセルクラスである:
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
}
}
IはtitleLabel
フレームを印刷するとき、それは所望のフレームのプロパティを印刷します。しかし、シミュレータでは、フレームはまだラベルに与えられたフレームであるlayoutSubviews
です。
setup
機能でself.layoutIfNeeded()
を試しましたが、機能しません。どのようにして、セルフサブビューを更新することができますか? cellForRowAtIndexPathで