Skip to content

Commit

Permalink
Merge pull request #37 from yukiasai/feature/estimated_height
Browse files Browse the repository at this point in the history
Estimated height
  • Loading branch information
xai3 authored Dec 28, 2016
2 parents b6b1d21 + 1fd0db3 commit 4447ab8
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 @@ -177,6 +177,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 {
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 4447ab8

Please sign in to comment.