All Projects → lixuansheng → MultiSelect

lixuansheng / MultiSelect

Licence: other
swift

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to MultiSelect

Squaremosaiclayout
An extandable mosaic UICollectionViewLayout with a focus on extremely flexible customizations 🔶
Stars: ✭ 243 (+1925%)
Mutual labels:  uitableview, uicollectionview
CollectionAndTableViewCompatible
A set of Swift protocols and Xcode snippets that will make it easy to do clean UITableView code
Stars: ✭ 34 (+183.33%)
Mutual labels:  uitableview, uicollectionview
IQListKit
Model driven UITableView/UICollectionView
Stars: ✭ 51 (+325%)
Mutual labels:  uitableview, uicollectionview
Rglistkit
RGListKit is a Protocol & MVVM based framework to easily populate a UITableView or UICollectionView via single api.
Stars: ✭ 178 (+1383.33%)
Mutual labels:  uitableview, uicollectionview
TinyCoordinator
The Swift version of ThinningCoordinator focus on lighter view controllers.
Stars: ✭ 18 (+50%)
Mutual labels:  uitableview, uicollectionview
Flowkit
A declarative type-safe framework for building fast and flexible list with Tables & Collection
Stars: ✭ 215 (+1691.67%)
Mutual labels:  uitableview, uicollectionview
XLRefresh
iOS 下拉刷新工具
Stars: ✭ 25 (+108.33%)
Mutual labels:  uitableview, uicollectionview
Genericdatasource
A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.
Stars: ✭ 127 (+958.33%)
Mutual labels:  uitableview, uicollectionview
react-multi-select-component
Lightweight (~5KB gzipped) multiple selection dropdown component
Stars: ✭ 279 (+2225%)
Mutual labels:  multiselect, checkboxes
PagedLists
Paginated UITableView and UICollectionViews for iOS.
Stars: ✭ 69 (+475%)
Mutual labels:  uitableview, uicollectionview
Hgplaceholders
Nice library to show placeholders and Empty States for any UITableView/UICollectionView in your project
Stars: ✭ 2,048 (+16966.67%)
Mutual labels:  uitableview, uicollectionview
ios ui recipe showcase
iOSアプリ開発 - UI実装であると嬉しいレシピブック掲載サンプル
Stars: ✭ 54 (+350%)
Mutual labels:  uitableview, uicollectionview
Vbpiledview
Simple and beautiful stacked UIView to use as a replacement for an UITableView, UIImageView or as a menu
Stars: ✭ 164 (+1266.67%)
Mutual labels:  uitableview, uicollectionview
Tabanimated
A skeleton screen framework based on native for iOS. (一个由iOS原生组件映射出骨架屏的框架,包含快速植入,低耦合,兼容复杂视图等特点,提供国内主流骨架屏动画的加载方案,同时支持上拉加载更多、自定制动画。)
Stars: ✭ 2,909 (+24141.67%)
Mutual labels:  uitableview, uicollectionview
Closures
Swifty closures for UIKit and Foundation
Stars: ✭ 1,720 (+14233.33%)
Mutual labels:  uitableview, uicollectionview
Cyanic
Declarative, state-driven UI framework
Stars: ✭ 32 (+166.67%)
Mutual labels:  uitableview, uicollectionview
Skeletonview
☠️ An elegant way to show users that something is happening and also prepare them to which contents they are awaiting
Stars: ✭ 10,804 (+89933.33%)
Mutual labels:  uitableview, uicollectionview
Gskstretchyheaderview
A generic stretchy header for UITableView and UICollectionView
Stars: ✭ 1,624 (+13433.33%)
Mutual labels:  uitableview, uicollectionview
HDEmptyView
一个Swift语言封装的EmptyView显示库,可作用于WKWebView、UITableView、UICollectionView 无网络提醒或者空数据提醒
Stars: ✭ 29 (+141.67%)
Mutual labels:  uitableview, uicollectionview
GenericCells
Creating generic UITableViewCells and UICollectionViewCells instead of subclasses.
Stars: ✭ 81 (+575%)
Mutual labels:  uitableview, uicollectionview

MultiSelect

ps:下载完代码,先pod install一下

最近在做项目,需要弄一个多层级多选(层级数量不定)的联动选择功能,本来想在github或者cocoachina找一下基本样例或者第三方库的,但是找到的都是限制层级的,一般为二层级、三层级,并不满足要求,于是自己抽时间写了一个,希望各位给出指点或意见。

1、实现思路 现在我实现的方式是上下两个UICollectionView。

a、 上面的UICollectionView(下面简称:organizationCollectionView)

主要负责层级的展开、收起、选择。由于层级是不确定的,用UIScrollView的话,层级过多,也无释放,内存会可能过高,所以这里选中UICollectionView实现每个UICollectionViewCell内再嵌套一个UITableView,

b、 下面的UICollectionView(下面简称:selectCollectionView),主要负责展现已经选中的层级,并且支持删除。

2、代码分析 a、多选简单使用

  let vc = GZTMultiSelectViewController()
  let navi = GZTNavigationViewController.init(rootViewController: vc)
  self.present(navi, animated: true) {}
  vc.selectBlock = { VC, departmentIds in
      VC.dismiss(animated: true, completion: {})
      textField.text = "  多选(\(departmentIds.count))"
  }

b、 单选简单使用

  let vc = GZTMultiSelectViewController()
  vc.type = .radio
  let navi = GZTNavigationViewController.init(rootViewController: vc)
  self.present(navi, animated: true) {}
  vc.selectBlock = { VC, departmentIds in
      VC.dismiss(animated: true, completion: {})
      textField.text = "  单选(\(departmentIds.count))"
  }

主要考虑是嵌套模型的model(model里面嵌套一个子model,但是两个model的数据格式是一样的,都是GZTModel)。为了解析这种类型数据,这里我引入了ObjectMapper,这个第三方库能有效帮我解析children这个数组,目前经测试,20个层次是没问题的。注意:记得导入ObjectMapper

c、organizationCollectionView和相关数据的初始化

fileprivate lazy var organizationCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    let collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.white
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.register(GZTOrganizationCell.self, forCellWithReuseIdentifier: organizationCell)
    collectionView.bounces = true
    return collectionView
}()
// 目录层次
fileprivate var organizationList: Array<Array<GZTModel>>? = []
// 进行的节点保存
fileprivate var organizationNode: [Int: GZTModel] = [:]

organizationCollectionView主要注意边距、布局方向的设置,重点是organizationList(数组)、organizationNode(字典) organizationList:主要用于存储后面organizationCollectionView每一个cell展开内部UITableView(后续简称tableView)的数据源 organizationNode:以当前的tableView的row作为键,存储和更新每次上述tableView中选择的model(每个cell对应的数据),用于后面寻找每一个被点击的model的父节点的model,刷新父节点名下被选中的个数。

d、selectOrganization() 主要实现的功能

    if (self.organizationList?.count)! - 1 <= indexPath.row {
        if model.children?.count != 0 {
            self.organizationList?.append(model.children!)
        }
    } else {
        let range = NSRange.init(location: indexPath.row + 1, length: (self.organizationList?.count)! - 1 - indexPath.row)
        self.organizationList?.removeSubrange(Range.init(range)!)
        if model.children?.count != 0 {
            self.organizationList?.append(model.children!)
        }
    }

主要是获取每次点击的model的children,即organizationCollectionView下一个cell展示的内容

   if indexPath.row > 0 && (self.organizationList?.count)! > 1{
        let superModel = self.organizationNode[indexPath.row - 1]
        superModel?.selectNum = 0
        superModel?.hasSelect = true
        //刷新上级
        for childrenModel in (superModel?.children)! {
            if childrenModel.hasSelect || childrenModel.selectNum > 0 {
                superModel?.selectNum = (superModel?.selectNum)! + 1
            }
        }
    }

在多选的情况下,主要是处理父节点的children中有几个别点击到的model,并记录

    if self.hasSelectOrganization != nil {
        self.hasSelectOrganization?.hasSelect = false
    }
    model.hasSelect = true
    self.hasSelectOrganization = model

单选的情况下,主要用于记录当前选中的数据model。

d、selectCollectionView和相关数据的初始化

fileprivate lazy var selectCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumInteritemSpacing = 5 * SCALE_WIDTH
    layout.minimumLineSpacing = 5  * SCALE_WIDTH
    layout.sectionInset = UIEdgeInsetsMake(6 * SCALE_HEIGHT, 12 * SCALE_WIDTH, 6 * SCALE_HEIGHT, 12 * SCALE_WIDTH)
    let collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.white
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.register(GZTSelectCollectionCell.self, forCellWithReuseIdentifier: selectCollectionCell)
    return collectionView
}()
fileprivate var selectList: Array<Array<GZTModel>>? = []

这里的主要需要注意selectList,这个数据是数组嵌套数组,外层主要代表被选择的个数;里面数据代表被选中的数据,从第一层数据到该数据的所有model。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].