Skip to content

Commit

Permalink
Estimated height
Browse files Browse the repository at this point in the history
  • Loading branch information
xai3 committed Dec 28, 2016
1 parent 35ea18a commit 3a381cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Classes/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ open class Row<T: UITableViewCell>: RowType {

open var configureCell: ((T, RowInformation) -> Void)?
open var heightFor: ((RowInformation) -> CGFloat?)?
open var estimatedHeightFor: ((RowInformation) -> CGFloat?)?
open var canRemove: ((RowInformation) -> Bool)?
open var canMove: ((RowInformation) -> Bool)?
open var canMoveTo: ((RowMoveInformation) -> Bool)?
Expand All @@ -45,6 +46,7 @@ open class Row<T: UITableViewCell>: RowType {
}

open var height: CGFloat?
open var estimatedHeight: CGFloat?
}

extension Row: RowDelegateType {
Expand All @@ -59,6 +61,10 @@ extension Row: RowDelegateType {
return heightFor?((self, tableView, indexPath)) ?? height
}

func estimatedHeightFor(_ tableView: UITableView, indexPath: IndexPath) -> CGFloat? {
return estimatedHeightFor?((self, tableView, indexPath)) ?? estimatedHeight
}

func canEdit(_ tableView: UITableView, indexPath: IndexPath) -> Bool {
return canRemove(tableView, indexPath: indexPath) || canMove(tableView, indexPath: indexPath)
}
Expand Down
1 change: 1 addition & 0 deletions Classes/RowType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public protocol RowType {
protocol RowDelegateType {
func configureCell(_ tableView: UITableView, cell: UITableViewCell, indexPath: IndexPath)
func heightFor(_ tableView: UITableView, indexPath: IndexPath) -> CGFloat?
func estimatedHeightFor(_ tableView: UITableView, indexPath: IndexPath) -> CGFloat?
func canEdit(_ tableView: UITableView, indexPath: IndexPath) -> Bool
func canRemove(_ tableView: UITableView, indexPath: IndexPath) -> Bool
func canMove(_ tableView: UITableView, indexPath: IndexPath) -> Bool
Expand Down
9 changes: 9 additions & 0 deletions Classes/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ extension Source: UITableViewDelegate {
return tableView.rowHeight
}

public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
let row = self.section(for: indexPath).rowFor(indexPath)
if let delegate = row as? RowDelegateType,
let estimatedHeight = delegate.estimatedHeightFor(tableView, indexPath: indexPath) {
return estimatedHeight
}
return tableView.estimatedRowHeight
}

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let header = self.section(for: section).header else {
return 0
Expand Down
18 changes: 18 additions & 0 deletions ShoyuTests/RowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ class RowTests: XCTestCase {
XCTAssertEqual(height, 10)
}

func testEstimatedHeightFor() {
let row = Row()

// Initialezed
XCTAssertEqual(row.estimatedHeightFor(UITableView(), indexPath: IndexPath()), nil)

// Constant
row.estimatedHeight = 15
XCTAssertEqual(row.estimatedHeightFor(UITableView(), indexPath: IndexPath()), 15)

// Configure
row.estimatedHeightFor = { _ -> CGFloat? in
return 30
}
let height = row.estimatedHeightFor(UITableView(), indexPath: IndexPath())
XCTAssertEqual(height, 30)
}

func testDidSelect() {
var called = false
let row = Row()
Expand Down

0 comments on commit 3a381cf

Please sign in to comment.