Skip to content

Commit

Permalink
Merge pull request #27 from bannzai/checkIndexPath
Browse files Browse the repository at this point in the history
Add: check permit index path
  • Loading branch information
xai3 committed Mar 31, 2016
2 parents 294d188 + 9704b7b commit 8f537d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Classes/Source.swift
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8f537d8

Please sign in to comment.