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
3 changes: 2 additions & 1 deletion SwiftMultiSelect/CustomTableCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Foundation
/// Class to represent custom cell for tableview
class CustomTableCell: UITableViewCell
{

var item:SwiftMultiSelectItem?

/// Lazy var for label title
open fileprivate(set) lazy var labelTitle: UILabel = {

Expand Down
18 changes: 11 additions & 7 deletions SwiftMultiSelect/MultiSelectionCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ extension MultiSelecetionViewController:UICollectionViewDelegate,UICollectionVie
SwiftMultiSelect.delegate?.swiftMultiSelect(didUnselectItem: item!)

//Reload cell state
reloadCellState(row: idp, selected: false)


if let item = item {
reloadCellState(row: idp, item: item, selected: false)
}


if selectedItems.count <= 0{
//Toggle scrollview
toggleSelectionScrollView(show: false)
Expand All @@ -175,10 +177,12 @@ extension MultiSelecetionViewController:UICollectionViewDelegate,UICollectionVie

//Scroll to selected item
self.selectionScrollView.scrollToItem(at: lastItemIndex, at: .right, animated: true)

reloadCellState(row: idp, selected: true)



if let item = item {
reloadCellState(row: idp, item: item, selected: true)
}


}


Expand Down
114 changes: 58 additions & 56 deletions SwiftMultiSelect/MultiSelectionTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ import Contacts

// MARK: - UITableViewDelegate,UITableViewDataSource
extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSource{

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return false
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if SwiftMultiSelect.dataSourceType == .phone{
if searchString == "" {
return SwiftMultiSelect.items!.count
}else{
return SwiftMultiSelect.items!.filter({$0.title.lowercased().contains(searchString.lowercased()) || ($0.description != nil && $0.description!.lowercased().contains(searchString.lowercased())) }).count
}
}else{

//Try to get rows from delegate
guard let rows = SwiftMultiSelect.dataSource?.numberOfItemsInSwiftMultiSelect() else {
return 0
}

return rows
}

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

//Get Reference to Cell
let cell : CustomTableCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableCell
cell.selectionStyle = .none

var item:SwiftMultiSelectItem!

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{
Expand All @@ -60,11 +60,11 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc
cell.labelSubTitle.text = item.description
cell.initials.isHidden = true
cell.imageAvatar.isHidden = true

if let contact = item.userInfo as? CNContact{

DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {

if(contact.imageDataAvailable && contact.imageData!.count > 0){
let img = UIImage(data: contact.imageData!)
DispatchQueue.main.async {
Expand All @@ -80,9 +80,9 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc
cell.imageAvatar.isHidden = true
}
}

}

}else{
if item.image == nil && item.imageURL == nil{
cell.initials.text = item.getInitials()
Expand All @@ -100,59 +100,62 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc
}
}
}

if item.color != nil{
cell.initials.backgroundColor = item.color!
}else{
cell.initials.backgroundColor = updateInitialsColorForIndexPath(indexPath)
}


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


return cell

}


/// Function that select a random color for passed indexpath
///
/// - Parameter indexpath:
/// - Returns: UIColor random, from Config.colorArray
func updateInitialsColorForIndexPath(_ indexpath: IndexPath) -> UIColor{

//Applies color to Initial Label
let randomValue = (indexpath.row + indexpath.section) % Config.colorArray.count

return Config.colorArray[randomValue]

}


/// Function to change accessoryType for passed index
///
/// - Parameters:
/// - row: index of row
/// - selected: true = chechmark, false = none
func reloadCellState(row:Int, selected:Bool){

if let cell = self.tableView.cellForRow(at: IndexPath(row: row, section: 0)) as? CustomTableCell{
cell.accessoryType = (selected) ? .checkmark : .none
func reloadCellState(row:Int, item: SwiftMultiSelectItem, selected:Bool){
if let cell = self.tableView.cellForRow(at: IndexPath(row: row, section: 0)) as? CustomTableCell {
var selectedAndFoundInTable = selected
if let cellItem = cell.item, let cellItemId = cellItem.id, let itemId = item.id {
selectedAndFoundInTable = selected && cellItemId == itemId
}
cell.accessoryType = selectedAndFoundInTable ? .checkmark : .none
}

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

//Get selected cell
let cell = tableView.cellForRow(at: indexPath) as! CustomTableCell

Expand All @@ -164,77 +167,76 @@ extension MultiSelecetionViewController:UITableViewDelegate,UITableViewDataSourc
//Try to get item from delegate
item = SwiftMultiSelect.dataSource?.swiftMultiSelect(itemAtRow: indexPath.row)
}
//Save item data

//Save item data
item.color = cell.initials.backgroundColor!

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

//Set accessory type
cell.accessoryType = UITableViewCellAccessoryType.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

//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)

}

//Reset search
if searchString != ""{
searchBar.text = ""
searchString = ""
SwiftMultiSelect.delegate?.userDidSearch(searchString: "")
SwiftMultiSelect.delegate?.userDidSearch(searchString: "", tableView: self.tableView)
self.tableView.reloadData()
}

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return CGFloat(Config.tableStyle.tableRowHeight)

}

// MARK: - UISearchBarDelegate

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchString = searchText

if (searchText.isEmpty) {
self.perform(#selector(self.hideKeyboardWithSearchBar(_:)), with: searchBar, afterDelay: 0)
self.searchString = ""
}

SwiftMultiSelect.delegate?.userDidSearch(searchString: searchText)

SwiftMultiSelect.delegate?.userDidSearch(searchString: searchText, tableView: self.tableView)

self.tableView.reloadData()
}

@objc func hideKeyboardWithSearchBar(_ searchBar:UISearchBar){
searchBar.resignFirstResponder()
}

func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool{
return true
}

}
Loading