From c94e5c405fc367b560af1a9b5ec24ba86cb5c978 Mon Sep 17 00:00:00 2001 From: Marcelo Romera Date: Tue, 26 Jul 2016 12:21:07 +1000 Subject: [PATCH] Add icon to left or right of the cell. --- DropDown/resources/DropDownCell.xib | 33 ++++++++++++++++++++++++----- DropDown/src/DropDown.swift | 33 +++++++++++++++++++++++++++++ DropDown/src/DropDownCell.swift | 17 +++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/DropDown/resources/DropDownCell.xib b/DropDown/resources/DropDownCell.xib index ebfed0b4..2a71910a 100644 --- a/DropDown/resources/DropDownCell.xib +++ b/DropDown/resources/DropDownCell.xib @@ -1,34 +1,57 @@ - + + - + + + + + + + + + + + + + - - + + + + + + + + + + + + diff --git a/DropDown/src/DropDown.swift b/DropDown/src/DropDown.swift index 6f06be72..8c90a478 100644 --- a/DropDown/src/DropDown.swift +++ b/DropDown/src/DropDown.swift @@ -71,6 +71,19 @@ public final class DropDown: UIView { case Bottom } + + /// The position of the icon for each drop down row. + public enum IconPosition { + + /// The drop down icon will not show. + case None + + /// The drop down icon will show on the left side of the row. + case Left + + /// The drop down icon will show on the right side of the row. + case Right + } //MARK: - Properties @@ -126,6 +139,12 @@ public final class DropDown: UIView { public var width: CGFloat? { didSet { setNeedsUpdateConstraints() } } + + /// The position of the icon to be shown on the row. + public var iconPosition: IconPosition? + + /// The icon image to be shown on the row. + public var icon: UIImage? //MARK: Constraints private var heightConstraint: NSLayoutConstraint! @@ -909,7 +928,21 @@ extension DropDown: UITableViewDataSource, UITableViewDelegate { cell.optionLabel.text = dataSource[index] } + if let position = iconPosition, let iconImage = icon { + switch position { + case .None: + cell.setupNoIcon() + case .Left: + cell.setupLeftIcon(iconImage) + case .Right: + cell.setupRightIcon(iconImage) + } + } else { + cell.setupNoIcon() + } + customCellConfiguration?(index, dataSource[index], cell) + return cell } diff --git a/DropDown/src/DropDownCell.swift b/DropDown/src/DropDownCell.swift index dd17ab9a..c2e6906d 100644 --- a/DropDown/src/DropDownCell.swift +++ b/DropDown/src/DropDownCell.swift @@ -12,6 +12,8 @@ public class DropDownCell: UITableViewCell { //UI @IBOutlet public weak var optionLabel: UILabel! + @IBOutlet public weak var leftIcon: UIImageView! + @IBOutlet public weak var rightIcon: UIImageView! var selectedBackgroundColor: UIColor? @@ -62,5 +64,20 @@ extension DropDownCell { executeSelection() } } + + public func setupNoIcon() { + leftIcon.removeFromSuperview() + rightIcon.removeFromSuperview() + } + + public func setupLeftIcon(icon: UIImage) { + rightIcon.removeFromSuperview() + leftIcon.image = icon + } + + public func setupRightIcon(icon: UIImage) { + leftIcon.removeFromSuperview() + rightIcon.image = icon + } } \ No newline at end of file