Fix dynamic type edit cell layout bug

This commit is contained in:
Maurice Parker 2019-04-28 12:08:50 -05:00
parent 2662352541
commit 95430913bd
2 changed files with 18 additions and 26 deletions

View File

@ -19,7 +19,6 @@ class MasterFeedTableViewCell : UITableViewCell {
weak var delegate: MasterFeedTableViewCellDelegate? weak var delegate: MasterFeedTableViewCellDelegate?
var allowDisclosureSelection = false var allowDisclosureSelection = false
private var layout: MasterFeedTableViewCellLayout?
override var accessibilityLabel: String? { override var accessibilityLabel: String? {
set {} set {}
@ -53,7 +52,6 @@ class MasterFeedTableViewCell : UITableViewCell {
var shouldShowImage = false { var shouldShowImage = false {
didSet { didSet {
if shouldShowImage != oldValue { if shouldShowImage != oldValue {
layout = nil
setNeedsLayout() setNeedsLayout()
} }
faviconImageView.image = shouldShowImage ? faviconImage : nil faviconImageView.image = shouldShowImage ? faviconImage : nil
@ -68,7 +66,6 @@ class MasterFeedTableViewCell : UITableViewCell {
if unreadCountView.unreadCount != newValue { if unreadCountView.unreadCount != newValue {
unreadCountView.unreadCount = newValue unreadCountView.unreadCount = newValue
unreadCountView.isHidden = (newValue < 1) unreadCountView.isHidden = (newValue < 1)
layout = nil
setNeedsLayout() setNeedsLayout()
} }
} }
@ -81,7 +78,6 @@ class MasterFeedTableViewCell : UITableViewCell {
set { set {
if titleView.text != newValue { if titleView.text != newValue {
titleView.text = newValue titleView.text = newValue
layout = nil
setNeedsLayout() setNeedsLayout()
} }
} }
@ -109,30 +105,22 @@ class MasterFeedTableViewCell : UITableViewCell {
commonInit() commonInit()
} }
override func prepareForReuse() {
layout = nil
unreadCountView.setNeedsLayout()
unreadCountView.setNeedsDisplay()
}
override func willTransition(to state: UITableViewCell.StateMask) { override func willTransition(to state: UITableViewCell.StateMask) {
super.willTransition(to: state) super.willTransition(to: state)
showingEditControl = state.contains(.showingEditControl) showingEditControl = state.contains(.showingEditControl)
} }
override func sizeThatFits(_ size: CGSize) -> CGSize { override func sizeThatFits(_ size: CGSize) -> CGSize {
if layout == nil { let shouldShowDisclosure = !(showingEditControl && showsReorderControl)
resetLayout() let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: shouldShowDisclosure)
} return CGSize(width: bounds.width, height: layout.height)
return CGSize(width: bounds.width, height: layout!.height)
} }
override func layoutSubviews() { override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
if layout == nil { let shouldShowDisclosure = !(showingEditControl && showsReorderControl)
resetLayout() let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: shouldShowDisclosure)
} layoutWith(layout)
layoutWith(layout!)
} }
@objc func buttonPressed(_ sender: UIButton) { @objc func buttonPressed(_ sender: UIButton) {
@ -184,11 +172,6 @@ private extension MasterFeedTableViewCell {
view.translatesAutoresizingMaskIntoConstraints = false view.translatesAutoresizingMaskIntoConstraints = false
} }
func resetLayout() {
let shouldShowDisclosure = !(showingEditControl && showsReorderControl)
layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: shouldShowDisclosure)
}
func layoutWith(_ layout: MasterFeedTableViewCellLayout) { func layoutWith(_ layout: MasterFeedTableViewCellLayout) {
faviconImageView.setFrameIfNotEqual(layout.faviconRect) faviconImageView.setFrameIfNotEqual(layout.faviconRect)
titleView.setFrameIfNotEqual(layout.titleRect) titleView.setFrameIfNotEqual(layout.titleRect)

View File

@ -19,6 +19,8 @@ struct MasterFeedTableViewCellLayout {
private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8) private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8)
private static let unreadCountMarginRight = CGFloat(integerLiteral: 0) private static let unreadCountMarginRight = CGFloat(integerLiteral: 0)
private static let disclosureButtonSize = CGSize(width: 44, height: 44) private static let disclosureButtonSize = CGSize(width: 44, height: 44)
private static let minRowHeight = CGFloat(integerLiteral: 44)
let faviconRect: CGRect let faviconRect: CGRect
let titleRect: CGRect let titleRect: CGRect
@ -64,9 +66,13 @@ struct MasterFeedTableViewCellLayout {
} }
// Title // Title
let labelWidth = bounds.width - (rFavicon.width + MasterFeedTableViewCellLayout.imageMarginRight + MasterFeedTableViewCellLayout.unreadCountMarginLeft + rUnread.width + MasterFeedTableViewCellLayout.unreadCountMarginRight + rDisclosure.width) let labelWidth = bounds.width - (rFavicon.width + MasterFeedTableViewCellLayout.imageMarginRight + MasterFeedTableViewCellLayout.unreadCountMarginLeft + rUnread.width + MasterFeedTableViewCellLayout.unreadCountMarginRight + MasterFeedTableViewCellLayout.disclosureButtonSize.width)
let labelSizeInfo = MultilineUILabelSizer.size(for: label.text ?? "", font: label.font, numberOfLines: 0, width: Int(floor(labelWidth))) let labelSizeInfo = MultilineUILabelSizer.size(for: label.text ?? "", font: label.font, numberOfLines: 0, width: Int(floor(labelWidth)))
if label.text == "inessential" {
print("Number of lines: \(labelSizeInfo.numberOfLinesUsed) height: \(labelSizeInfo.size.height)")
}
var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSizeInfo.size.width, height: labelSizeInfo.size.height) var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSizeInfo.size.width, height: labelSizeInfo.size.height)
if shouldShowImage { if shouldShowImage {
rLabel.origin.x = rFavicon.maxX + MasterFeedTableViewCellLayout.imageMarginRight rLabel.origin.x = rFavicon.maxX + MasterFeedTableViewCellLayout.imageMarginRight
@ -75,8 +81,11 @@ struct MasterFeedTableViewCellLayout {
} }
// Determine cell height // Determine cell height
let cellHeight = [rFavicon, rLabel, rUnread, rDisclosure].maxY() var cellHeight = [rFavicon, rLabel, rUnread, rDisclosure].maxY()
if cellHeight < MasterFeedTableViewCellLayout.minRowHeight {
cellHeight = MasterFeedTableViewCellLayout.minRowHeight
}
// Center in Cell // Center in Cell
let newBounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width, height: cellHeight) let newBounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width, height: cellHeight)