Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from realm-demos/mt/imagecelllayoutreentrancy
Browse files Browse the repository at this point in the history
Fixes a bug when several image cells are resizing at the same time
  • Loading branch information
icanzilb authored Aug 31, 2017
2 parents fdbcfca + 7c5fdcb commit 80b02ba
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions RealmContent/Core/View/ContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ public class ContentViewController: UIViewController, UITableViewDataSource, UIT
return page.elements.count
}

internal var refreshId: UInt64 = 0
internal func throttledLayoutUpdate() {
refreshId+=1
let currentId = refreshId
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
guard let this = self, currentId == this.refreshId else { return }
this.tableView.beginUpdates()
this.tableView.endUpdates()
}
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let element = page.elements[indexPath.row]

Expand All @@ -199,8 +210,7 @@ public class ContentViewController: UIViewController, UITableViewDataSource, UIT
let cellId = String(describing: ImageContentCell.self)
let cell: ImageContentCell = tableView.dequeueReusableCell(withIdentifier: cellId) as! ImageContentCell
cell.populate(with: element) { [weak self] in
self?.tableView.beginUpdates()
self?.tableView.endUpdates()
self?.throttledLayoutUpdate()
}
cell.delegate = self
return cell
Expand Down

0 comments on commit 80b02ba

Please sign in to comment.