From 3fa668604ba124a66bbb6c7967d91e2edce5d399 Mon Sep 17 00:00:00 2001 From: Mark Adams Date: Thu, 22 Oct 2015 22:47:30 -0700 Subject: [PATCH 1/2] Make height optional. Fallback to `UITableView.rowHeight` We now make no assumptions about a row height. If `height` is nil, we'll return `UITableView.rowHeight`. For AutoLayout cells, you must explicitly specify a `height` of `UITableViewAutomaticDimension`. --- Static/DataSource.swift | 14 ++++++++------ Static/Row.swift | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Static/DataSource.swift b/Static/DataSource.swift index bf24807..5f3c182 100644 --- a/Static/DataSource.swift +++ b/Static/DataSource.swift @@ -187,17 +187,19 @@ extension DataSource: UITableViewDataSource { } public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - return rowForIndexPath(indexPath)?.height ?? UITableViewAutomaticDimension + guard let height = rowForIndexPath(indexPath)?.height else { + return tableView.rowHeight + } + + return height } public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { - guard let row = rowForIndexPath(indexPath) else { return UITableViewAutomaticDimension } - - if row.height == UITableViewAutomaticDimension { - return 44 + guard let height = rowForIndexPath(indexPath)?.height else { + return UITableViewAutomaticDimension } - return row.height + return height } public func numberOfSectionsInTableView(tableView: UITableView) -> Int { diff --git a/Static/Row.swift b/Static/Row.swift index ef7993c..515050e 100644 --- a/Static/Row.swift +++ b/Static/Row.swift @@ -111,8 +111,8 @@ public struct Row: Hashable, Equatable { /// View to be used for the row. public var cellClass: CellType.Type - /// The row's height. Defaults to `UITableViewAutomaticDimension`. - public var height: CGFloat + /// The row's height. + public var height: CGFloat? /// Additional information for the row. public var context: Context? @@ -140,7 +140,7 @@ public struct Row: Hashable, Equatable { // MARK: - Initializers public init(text: String? = nil, detailText: String? = nil, selection: Selection? = nil, - image: UIImage? = nil, imageTintColor: UIColor? = nil, accessory: Accessory = .None, cellClass: CellType.Type? = nil, height: CGFloat = UITableViewAutomaticDimension, context: Context? = nil, editActions: [EditAction] = [], UUID: String = NSUUID().UUIDString) { + image: UIImage? = nil, imageTintColor: UIColor? = nil, accessory: Accessory = .None, cellClass: CellType.Type? = nil, height: CGFloat? = nil, context: Context? = nil, editActions: [EditAction] = [], UUID: String = NSUUID().UUIDString) { self.UUID = UUID self.text = text From dbc6bc3734d5a19d2e7a011464ca1b069dab96d8 Mon Sep 17 00:00:00 2001 From: Mark Adams Date: Thu, 22 Oct 2015 22:48:43 -0700 Subject: [PATCH 2/2] Update example --- Example/ViewController.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 771b2aa..eacae51 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -25,6 +25,8 @@ class ViewController: TableViewController { super.viewDidLoad() title = "Static" + + tableView.rowHeight = 66 dataSource.sections = [ Section(header: "Styles", rows: [ @@ -36,8 +38,8 @@ class ViewController: TableViewController { Row(text: "Button", detailText: "Detail", cellClass: ButtonCell.self, selection: { [unowned self] in self.showAlert(title: "Row Selection") }), - Row(text: "Custom cell with explicit height", cellClass: CustomTableViewCell.self, height: 64), - Row(text: "Custom from nib", cellClass: NibTableViewCell.self) + Row(text: "Custom cell with explicit height", cellClass: CustomTableViewCell.self, height: 44), + Row(text: "Custom from nib", cellClass: NibTableViewCell.self, height: UITableViewAutomaticDimension) ], footer: "This is a section footer."), Section(header: "Accessories", rows: [ Row(text: "None"),