Моя проблема в том, что я хочу вставить contentView ячейки на 10 для каждого верхнего, нижнего, левого, правого.Я хочу использовать это пространство в качестве разделителя и отключить разделитель табличного представления по умолчанию.Это выглядит так без вставок:
Теперь есть две проблемы:
- Проблема: Неоднозначные ограничения по высоте (в представлении отладчик)
- Без разделителя
1.
Я могу изменить это, переопределив layoutSubviews для моей ячейки табличного представления:
override func layoutSubviews() {
super.layoutSubviews()
let contentViewFrame = self.contentView.frame
let insetContentViewFrame = contentViewFrame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
self.contentView.frame = insetContentViewFrame
self.selectedBackgroundView?.frame = insetContentViewFrame
}
Однако это вызывает проблему номер 3:
Теперь у метки описания есть проблемы:
Вот моя ячейка табличного представления (которая использует autolayout, поэтому метки определяют высоту ячейки):
class NewsTableViewCell : UITableViewCell {
public private(set) var titleLabel: UILabel!
public private(set) var descriptionLabel: UILabel!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.tintColor = UIColor.white
self.contentView.backgroundColor = UIColor.barTintColor
self.contentView.layer.masksToBounds = false
self.contentView.layer.cornerRadius = 10.0
self.backgroundColor = UIColor.clear
let layoutGuide = self.contentView.layoutMarginsGuide
self.titleLabel = UILabel(frame: .zero)
self.titleLabel.numberOfLines = 0
self.titleLabel.font = UIFont.preferredFont(forTextStyle: .headline)
self.titleLabel.textColor = UIColor.white
self.titleLabel.adjustsFontSizeToFitWidth = false
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.titleLabel.textAlignment = .left
self.contentView.addSubview(self.titleLabel)
self.titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)
self.titleLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
self.titleLabel.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
self.titleLabel.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
self.descriptionLabel = UILabel(frame: .zero)
self.descriptionLabel.numberOfLines = 0
self.descriptionLabel.font = UIFont.preferredFont(forTextStyle: .subheadline)
self.descriptionLabel.textColor = UIColor.lightGray
self.descriptionLabel.adjustsFontSizeToFitWidth = false
self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
self.descriptionLabel.textAlignment = .left
self.contentView.addSubview(self.descriptionLabel)
self.descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)
self.descriptionLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
self.descriptionLabel.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
self.descriptionLabel.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: 5.0).isActive = true
self.descriptionLabel.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
let selectedView: UIView = UIView(frame: .zero)
selectedView.backgroundColor = UIColor.gray
selectedView.layer.cornerRadius = 10.0
selectedView.layer.masksToBounds = false
self.selectedBackgroundView = selectedView
}
Это код контроллера моего табличного представления:
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 200.0
self.tableView.register(NewsTableViewCell.self, forCellReuseIdentifier: "NewsCell")
self.tableView.separatorStyle = .none
Можно ли как-то вставить мой contentView и иметь метки динамического размера (которые определяют высоту ячейки)?Спасибо!