diff --git a/ThunderTable/TableRow.swift b/ThunderTable/TableRow.swift index 8d0b782..a3e729c 100644 --- a/ThunderTable/TableRow.swift +++ b/ThunderTable/TableRow.swift @@ -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 @@ -168,6 +171,10 @@ extension Row { return nil } + public var placeholderImage: UIImage? { + return nil + } + public var image: UIImage? { get { return nil } set {} @@ -253,6 +260,8 @@ open class TableRow: Row { open var image: UIImage? + open var placeholderImage: UIImage? + open var imageSize: CGSize? open var imageURL: URL? { diff --git a/ThunderTable/TableViewController.swift b/ThunderTable/TableViewController.swift index 15fcf8f..35afe33 100644 --- a/ThunderTable/TableViewController.swift +++ b/ThunderTable/TableViewController.swift @@ -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) } }) diff --git a/ThunderTableDemo/ViewController.swift b/ThunderTableDemo/ViewController.swift index 5597338..0e4085e 100644 --- a/ThunderTableDemo/ViewController.swift +++ b/ThunderTableDemo/ViewController.swift @@ -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 @@ -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 }