Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: check permit index path #27

Merged
merged 1 commit into from Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Classes/Source.swift
Expand Up @@ -22,6 +22,10 @@ public class Source: NSObject {

public var didMoveRow: ((NSIndexPath, NSIndexPath) -> Void)?

func isPermitIndexPath(indexPath: NSIndexPath) -> Bool {
return sections.count > indexPath.section && sections[indexPath.section].rows.count > indexPath.row
}

public func addSection(section: SectionType) -> Self {
sections.append(section)
return self
Expand Down Expand Up @@ -200,6 +204,9 @@ extension Source: UITableViewDelegate {
}

public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !isPermitIndexPath(indexPath) {
return
}
let row = sectionFor(indexPath).rowFor(indexPath) as? RowDelegateType
row?.didEndDisplayCell(tableView, cell: cell, indexPath: indexPath)
}
Expand Down
17 changes: 17 additions & 0 deletions ShoyuTests/SourceTests.swift
Expand Up @@ -120,6 +120,23 @@ class SourceTests: XCTestCase {
XCTAssertEqual(source.sectionFor(destinationIndexPath).rowFor(destinationIndexPath).reuseIdentifier, reuserIdentifierFromIndexPath(sourceIndexPath))
}

func testPermitIndexPath() {
let rowLimit = UInt(10)
let sectionLimit = UInt(10)

let source = Source() { source in
source.createSections(sectionLimit) { _, section in
section.createRows(rowLimit) { _ in }
}
}

let notFailIndexPath = NSIndexPath(forRow: Int(rowLimit - 1), inSection: Int(sectionLimit - 1))
XCTAssertTrue(source.isPermitIndexPath(notFailIndexPath))

let failIndexPath = NSIndexPath(forRow: Int(rowLimit), inSection: Int(sectionLimit))
XCTAssertFalse(source.isPermitIndexPath(failIndexPath))
}

func testBenchmarkSource() {
class HeaderView: UIView { }
class FooterView: UIView { }
Expand Down