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

Adds placeholderImage property to Row protocol #61

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ThunderTable/TableRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public protocol Row {
/// An image to be displayed in the row
var image: UIImage? { get set }

/// An image to be used as placeholder when using asynchronous image loading and `image` is nil
var placeholderImage: UIImage? { get }

/// The size of the image which will be displayed in the row
///
/// This will be used when displaying an image using imageURL in order
Expand Down Expand Up @@ -168,6 +171,10 @@ extension Row {
return nil
}

public var placeholderImage: UIImage? {
return nil
}

public var image: UIImage? {
get { return nil }
set {}
Expand Down Expand Up @@ -253,6 +260,8 @@ open class TableRow: Row {

open var image: UIImage?

open var placeholderImage: UIImage?

open var imageSize: CGSize?

open var imageURL: URL? {
Expand Down
12 changes: 8 additions & 4 deletions ThunderTable/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,15 @@ open class TableViewController: UITableViewController, UIContentSizeCategoryAdju
size = imageSize
}

imageView?.set(imageURL: row.imageURL, withPlaceholder: row.image, imageSize: size, animated: true, completion: { [weak self] (image, error) -> (Void) in

if let welf = self, _row.image == nil {
imageView?.set(
imageURL: row.imageURL,
withPlaceholder: row.image ?? row.placeholderImage,
imageSize: size,
animated: true,
completion: { [weak self] (image, error) -> (Void) in
if _row.image == nil {
_row.image = image
welf.tableView.reloadRows(at: [indexPath], with: .none)
self?.tableView.reloadRows(at: [indexPath], with: .none)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexPath might not be the correct indexPath when this method comes back

}
})

Expand Down
7 changes: 6 additions & 1 deletion ThunderTableDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class ViewController: TableViewController {
let imageRow = TableRow(title: "Bundled Image", subtitle: "With Footer", image: #imageLiteral(resourceName: "logo"), selectionHandler: nil)
let remoteImageRow = TableRow(title: "Remote Image")
remoteImageRow.imageURL = URL(string: "http://via.placeholder.com/120x80")
remoteImageRow.imageSize = .init(width: 120, height: 80)

let remoteImageWithPlaceholderRow = TableRow(title: "Remote Image w/ Placeholder")
remoteImageWithPlaceholderRow.placeholderImage = #imageLiteral(resourceName: "logo")
remoteImageWithPlaceholderRow.imageURL = URL(string: "http://via.placeholder.com/80x80")

let actionRow = TableRow(title: "Show Alert", subtitle: nil, image: nil) { (row, selected, indexPath, tableView) -> (Void) in

Expand All @@ -79,7 +84,7 @@ class ViewController: TableViewController {
self.present(alertViewController, animated: true, completion: nil)
}

let basicsSection = TableSection(rows: [row, subtitleRow, imageRow, remoteImageRow, actionRow], header: "Header", footer: "Footer", selectionHandler: nil)
let basicsSection = TableSection(rows: [row, subtitleRow, imageRow, remoteImageRow, remoteImageWithPlaceholderRow, actionRow], header: "Header", footer: "Footer", selectionHandler: nil)

return basicsSection
}
Expand Down