Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.DS_Store
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
5.0
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<img src="https://user-images.githubusercontent.com/16253548/28709765-d82d454a-7381-11e7-8cd6-8d34f5c4cfd3.png" width=400px alt="SwiftMultiSelect" title="SwiftMultiSelect">
</p>

[![Version](https://img.shields.io/badge/pod-0.2.4-blue.svg)](https://cocoapods.org/pods/InAppNotify) [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://cocoapods.org/pods/InAppNotify) [![Platform](https://img.shields.io/badge/platform-ios-lightgrey.svg)](https://cocoapods.org/pods/InAppNotify) [![Swift3](https://img.shields.io/badge/swift3-compatible-brightgreen.svg)](https://cocoapods.org/pods/InAppNotify)
[![Version](https://img.shields.io/badge/pod-0.2.4-blue.svg)](https://cocoapods.org/pods/SwiftMultiSelect) [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://cocoapods.org/pods/SwiftMultiSelect) [![Platform](https://img.shields.io/badge/platform-ios-lightgrey.svg)](https://cocoapods.org/pods/SwiftMultiSelect) [![Swift5](https://img.shields.io/badge/swift5-compatible-brightgreen.svg)](https://cocoapods.org/pods/SwiftMultiSelect)

During develop of my apps, i usually needed to select more than one element from a tableview, but i don't like the native ios integration, i think is not graphically clear, so, i created this library. Choose SwiftMultiSelect for your next project, I'll be happy to give you a little help!

Expand All @@ -30,7 +30,7 @@ Try our demo on [appetize.io](https://appetize.io/embed/4zz7rwje6npp03d8u8d8a75q
## Requirements

- iOS 9+
- swift 3.0
- swift 5.0
- Access for Contacts

## Main features
Expand Down
2 changes: 1 addition & 1 deletion SwiftMultiSelect/CustomTableCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CustomTableCell: UITableViewCell
/// - Parameters:
/// - style: style for cell
/// - reuseIdentifier: string for reuse
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!)
{
//First Call Super
super.init(style: style, reuseIdentifier: reuseIdentifier)
Expand Down
2 changes: 1 addition & 1 deletion SwiftMultiSelect/MultiSelectionCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ extension MultiSelecetionViewController:UICollectionViewDelegate,UICollectionVie
if remove {

//For remove from collection view and create IndexPath, i need the index posistion in the array
let id = selectedItems.index { (itm) -> Bool in
let id = selectedItems.firstIndex { (itm) -> Bool in
itm.row == idp
}

Expand Down
49 changes: 29 additions & 20 deletions SwiftMultiSelect/MultiSelectionTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc


//Set initial state
if let itm_pre = self.selectedItems.index(where: { (itm) -> Bool in
if let itm_pre = self.selectedItems.firstIndex(where: { (itm) -> Bool in
itm == item
}){
self.selectedItems[itm_pre].color = cell.initials.backgroundColor!
cell.accessoryType = UITableViewCellAccessoryType.checkmark
cell.accessoryType = UITableViewCell.AccessoryType.checkmark
}else{
cell.accessoryType = UITableViewCellAccessoryType.none
cell.accessoryType = UITableViewCell.AccessoryType.none
}


Expand Down Expand Up @@ -160,7 +160,7 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc

if SwiftMultiSelect.dataSourceType == .phone{
item = (searchString == "") ? SwiftMultiSelect.items![indexPath.row] : SwiftMultiSelect.items!.filter({$0.title.lowercased().contains(searchString.lowercased()) || ($0.description != nil && $0.description!.lowercased().contains(searchString.lowercased())) })[indexPath.row]
}else{
} else {
//Try to get item from delegate
item = SwiftMultiSelect.dataSource?.swiftMultiSelect(itemAtRow: indexPath.row)
}
Expand All @@ -169,32 +169,41 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc
item.color = cell.initials.backgroundColor!

//Check if cell is already selected or not
if cell.accessoryType == UITableViewCellAccessoryType.checkmark
{
if cell.accessoryType == UITableViewCell.AccessoryType.checkmark {

//Set accessory type
cell.accessoryType = UITableViewCellAccessoryType.none
cell.accessoryType = UITableViewCell.AccessoryType.none

//Comunicate deselection to delegate
SwiftMultiSelect.delegate?.swiftMultiSelect(didUnselectItem: item)

//Reload collectionview
self.reloadAndPositionScroll(idp: item.row!, remove:true)

}
else{

//Set accessory type
cell.accessoryType = UITableViewCellAccessoryType.checkmark
} else {

//Add current item to selected
selectedItems.append(item)
if Config.maxSelectItems != -1 && selectedItems.count >= Config.maxSelectItems - 1 {

//Max number reached
SwiftMultiSelect.delegate?.numberMaximumOfItemsReached(items: selectedItems)

}

//Comunicate selection to delegate
SwiftMultiSelect.delegate?.swiftMultiSelect(didSelectItem: item)

//Reload collectionview
self.reloadAndPositionScroll(idp: item.row!, remove:false)
if Config.maxSelectItems == -1 || selectedItems.count < Config.maxSelectItems { //Default value = -1

//Set accessory type
cell.accessoryType = UITableViewCell.AccessoryType.checkmark

//Add current item to selected
selectedItems.append(item)

//Comunicate selection to delegate
SwiftMultiSelect.delegate?.swiftMultiSelect(didSelectItem: item)

//Reload collectionview
self.reloadAndPositionScroll(idp: item.row!, remove:false)

}

}

Expand All @@ -219,7 +228,7 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchString = searchText

if (searchText.isEmpty) {
if searchText.isEmpty {
self.perform(#selector(self.hideKeyboardWithSearchBar(_:)), with: searchBar, afterDelay: 0)
self.searchString = ""
}
Expand Down
17 changes: 12 additions & 5 deletions SwiftMultiSelect/MultiSelectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class MultiSelecetionViewController: UIViewController,UIGestureRecognizerDelegat
//Build layout
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.itemSize = CGSize(width: CGFloat(Config.selectorStyle.selectionHeight),height: CGFloat(Config.selectorStyle.selectionHeight));

//Build collectin view
let selected = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
Expand Down Expand Up @@ -149,19 +150,25 @@ class MultiSelecetionViewController: UIViewController,UIGestureRecognizerDelegat
//constraint for stackview
let stackView_H = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-0-[stackView]-0-|",
options: NSLayoutFormatOptions(rawValue: 0),
options: NSLayoutConstraint.FormatOptions(rawValue: 0),
metrics: nil,
views: viewsDictionary
)
//constraint for stackview
let stackView_V = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-\(navBarHeight)-[stackView]-0-|",
options: NSLayoutFormatOptions(rawValue:0),
withVisualFormat: "V:|-[stackView]-0-|",
options: NSLayoutConstraint.FormatOptions(rawValue:0),
metrics: nil,
views: viewsDictionary
)

searchBar.topAnchor.constraint(equalTo: stackView.topAnchor, constant: 0).isActive = true
// Test if has notch
if (UIApplication.isDeviceWithSafeArea) {
searchBar.topAnchor.constraint(equalTo: stackView.topAnchor, constant: 25).isActive = true
} else {
searchBar.topAnchor.constraint(equalTo: stackView.topAnchor, constant: 0).isActive = true
}

searchBar.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
searchBar.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: CGFloat(40)).isActive = true
Expand Down
24 changes: 19 additions & 5 deletions SwiftMultiSelect/SwiftMultiSelect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ public class SwiftMultiSelect{

/// Public struct for configuration and customizations
public struct Config {


/// Number max of items that can be selected. Initial value -1 = set to false, others = true
public static var maxSelectItems : Int = -1
/// Background of main view
public static var mainBackground : UIColor = UIColor.white
/// View's title
public static var viewTitle : String = "Swift Multiple Select"
/// Title for done button
public static var doneString : String = "Done"
public static var doneString : String = "Valider"
//Placeholder image during lazy load
public static var placeholder_image : UIImage = SwiftMultiSelect.image(named: "user_blank")!
/// Array of colors to use in initials
Expand Down Expand Up @@ -298,13 +300,14 @@ public protocol SwiftMultiSelectDelegate{
/// Tell to delegate that item has been unselected
func swiftMultiSelect(didUnselectItem item:SwiftMultiSelectItem)

/// Tell to delegate that the number of item selected has been reached
func numberMaximumOfItemsReached(items: [SwiftMultiSelectItem])

/// Tell to delegate user has closed without select
func didCloseSwiftMultiSelect()

/// Tell to delegate user has closed without select
func userDidSearch(searchString:String)


}

// MARK: - UIImageView
Expand Down Expand Up @@ -340,5 +343,16 @@ extension UIImageView{
}
}


extension UIApplication {

static var isDeviceWithSafeArea:Bool {
if #available(iOS 11.0, *) {
if let topPadding = shared.keyWindow?.safeAreaInsets.bottom,
topPadding > 0 {
return true
}
}
return false
}
}

Loading