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

Support for images in dropdown cells #65

Open
wants to merge 5 commits into
base: master
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
6 changes: 6 additions & 0 deletions Demo/Demo/Images.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
21 changes: 21 additions & 0 deletions Demo/Demo/Images.xcassets/star.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "star.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file added Demo/Demo/Images.xcassets/star.imageset/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 14 additions & 4 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let items = ["Most Popular", "Latest", "Trending", "Nearest", "Top Picks"]
self.selectedCellLabel.text = items.first

let image = UIImage(named: "star.png")

let items = [
BTItem(image: nil, title: "Most Popular"),
BTItem(image: image, title: "Latest"),
BTItem(image: nil, title: "Trending"),
BTItem(image: image, title: "Nearest"),
BTItem(image: image, title: "Top Picks")
]

self.selectedCellLabel.text = items.first?.title
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.0/255.0, green:180/255.0, blue:220/255.0, alpha: 1.0)
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, title: items[1], items: items)
menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, title: items[0].title, items: items)
menuView.cellHeight = 50
menuView.cellBackgroundColor = self.navigationController?.navigationBar.barTintColor
menuView.cellSelectionColor = UIColor(red: 0.0/255.0, green:160.0/255.0, blue:195.0/255.0, alpha: 1.0)
Expand All @@ -35,7 +45,7 @@ class ViewController: UIViewController {
menuView.maskBackgroundOpacity = 0.3
menuView.didSelectItemAtIndexHandler = {(indexPath: Int) -> () in
print("Did select item at index: \(indexPath)")
self.selectedCellLabel.text = items[indexPath]
self.selectedCellLabel.text = items[indexPath].title
}

self.navigationItem.titleView = menuView
Expand Down
45 changes: 34 additions & 11 deletions Source/BTNavigationDropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,19 @@ public class BTNavigationDropdownMenu: UIView {
private var menuArrow: UIImageView!
private var backgroundView: UIView!
private var tableView: BTTableView!
private var items: [AnyObject]!
private var items: [BTItem]!
private var menuWrapper: UIView!

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@available(*, deprecated, message="Use init(navigationController:title:items:) instead", renamed="BTNavigationDropdownMenu(navigationController: UINavigationController?, title: String, items: [AnyObject])")
public convenience init(title: String, items: [AnyObject]) {
public convenience init(title: String, items: [BTItem]) {
self.init(navigationController: nil, title: title, items: items)
}

public init(navigationController: UINavigationController? = nil, title: String, items: [AnyObject]) {
public init(navigationController: UINavigationController? = nil, title: String, items: [BTItem]) {

// Navigation controller
if let navigationController = navigationController {
Expand Down Expand Up @@ -261,7 +261,7 @@ public class BTNavigationDropdownMenu: UIView {

self.tableView.selectRowAtIndexPathHandler = { [weak self] (indexPath: Int) -> () in
self?.didSelectItemAtIndexHandler!(indexPath: indexPath)
self?.setMenuTitle("\(items[indexPath])")
self?.setMenuTitle("\(items[indexPath].title)")
self?.hideMenu()
self?.layoutSubviews()
}
Expand Down Expand Up @@ -470,18 +470,18 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
var selectRowAtIndexPathHandler: ((indexPath: Int) -> ())?

// Private properties
private var items: [AnyObject]!
private var items: [BTItem]!
private var selectedIndexPath: Int!

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

init(frame: CGRect, items: [AnyObject], title: String, configuration: BTConfiguration) {
init(frame: CGRect, items: [BTItem], title: String, configuration: BTConfiguration) {
super.init(frame: frame, style: UITableViewStyle.Plain)

self.items = items
self.selectedIndexPath = (items as! [String]).indexOf(title)
self.selectedIndexPath = items.indexOf() { $0.title == title }
self.configuration = configuration

// Setup table view
Expand Down Expand Up @@ -515,8 +515,12 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = BTTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell", configuration: self.configuration)
cell.textLabel?.text = self.items[indexPath.row] as? String
let item = self.items[indexPath.row]
let cell = BTTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell", configuration: self.configuration, item: item)

cell.imageView?.image = item.image
cell.textLabel?.text = item.title

cell.checkmarkIcon.hidden = (indexPath.row == selectedIndexPath) ? false : true
if self.configuration.keepSelectedCellColor == true {
cell.contentView.backgroundColor = (indexPath.row == selectedIndexPath) ? self.configuration.cellSelectionColor : self.configuration.cellBackgroundColor
Expand Down Expand Up @@ -545,12 +549,13 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
class BTTableViewCell: UITableViewCell {
let checkmarkIconWidth: CGFloat = 50
let horizontalMargin: CGFloat = 20
let imageMargin: CGFloat = 10

var checkmarkIcon: UIImageView!
var cellContentFrame: CGRect!
var configuration: BTConfiguration!

init(style: UITableViewCellStyle, reuseIdentifier: String?, configuration: BTConfiguration) {
init(style: UITableViewCellStyle, reuseIdentifier: String?, configuration: BTConfiguration, item: BTItem) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

self.configuration = configuration
Expand All @@ -559,13 +564,21 @@ class BTTableViewCell: UITableViewCell {
cellContentFrame = CGRectMake(0, 0, (UIApplication.sharedApplication().keyWindow?.frame.width)!, self.configuration.cellHeight)
self.contentView.backgroundColor = self.configuration.cellBackgroundColor
self.selectionStyle = UITableViewCellSelectionStyle.None

self.imageView?.frame = CGRectMake(horizontalMargin, (cellContentFrame.height - 30)/2, 30, 30)
self.imageView?.contentMode = .ScaleAspectFit

self.textLabel!.textColor = self.configuration.cellTextLabelColor
self.textLabel!.font = self.configuration.cellTextLabelFont
self.textLabel!.textAlignment = self.configuration.cellTextLabelAlignment
if self.textLabel!.textAlignment == .Center {
self.textLabel!.frame = CGRectMake(0, 0, cellContentFrame.width, cellContentFrame.height)
} else if self.textLabel!.textAlignment == .Left {
self.textLabel!.frame = CGRectMake(horizontalMargin, 0, cellContentFrame.width, cellContentFrame.height)
var x = horizontalMargin
if let _ = item.image, width = imageView?.frame.size.width {
x = x + width + imageMargin
}
self.textLabel!.frame = CGRectMake(x, 0, cellContentFrame.width, cellContentFrame.height)
} else {
self.textLabel!.frame = CGRectMake(-horizontalMargin, 0, cellContentFrame.width, cellContentFrame.height)
}
Expand Down Expand Up @@ -601,6 +614,16 @@ class BTTableViewCell: UITableViewCell {
}
}

public struct BTItem {
public let image: UIImage?
public let title: String

public init(image: UIImage?, title: String) {
self.image = image
self.title = title
}
}

// Content view of table view cell
class BTTableCellContentView: UIView {
var separatorColor: UIColor = UIColor.blackColor()
Expand Down