Skip to content

Commit

Permalink
Merge pull request #177 from logannathanson/verticals/10951009/Reset-…
Browse files Browse the repository at this point in the history
…Image-Before-Reuse

Image view now shows the correct image when the cell is reused
  • Loading branch information
jay18001 authored Aug 28, 2017
2 parents 17bfb7d + 035ca8b commit e732c03
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Source/Bricks/Image/ImageBrick.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ open class ImageBrickCell: GenericBrickCell, Bricklike, AsynchronousResizableCel
imageView.image = nil
}
}

override open func prepareForReuse() {
super.prepareForReuse()
imageView.image = nil
currentImageURL = nil
imageLoaded = false
}


fileprivate func resize(image: UIImage) {
Expand Down
34 changes: 34 additions & 0 deletions Tests/Bricks/ImageBrickTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,40 @@ class ImageBrickTests: XCTestCase {

wait(for: [expectation, expectationDidUpdate], timeout: 2)
}

func testImageResetsBeforeReusing() {
var expectation: XCTestExpectation? = self.expectation(description: "testImageResetsBeforeReusing - Wait for image to download")

let fixedImageDownloader = FixedNSURLSessionImageDownloader { (success) in
DispatchQueue.main.async {
expectation?.fulfill()
expectation = nil
}
}

BrickCollectionView.imageDownloader = fixedImageDownloader

brickView.registerBrickClass(ImageBrick.self)

let section = BrickSection(bricks: [
ImageBrick("imageBrick", dataSource: ImageURLBrickModel(url: imageURL, contentMode: .scaleAspectFill)),
])

brickView.setSection(section)
brickView.layoutSubviews()

let cell1 = brickView.cellForItem(at: IndexPath(item: 0, section: 1)) as? ImageBrickCell
cell1?.imageView.image = self.image
cell1?.layoutIfNeeded()

waitForExpectations(timeout: 10, handler: nil)


XCTAssertFalse(cell1?.imageView.image == nil)
cell1?.prepareForReuse()
XCTAssertTrue(cell1?.imageView.image == nil)

}
}

class FixedNSURLSessionImageDownloader: NSURLSessionImageDownloader {
Expand Down

0 comments on commit e732c03

Please sign in to comment.