RSSelectionMenu 7.1.3

RSSelectionMenu 7.1.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2021年5月
SPM支持 SPM

维护者:Rushi Sangani.




  • 作者:
  • Rushi Sangani

RSSelectionMenu

一个优雅的 iOS 选择列表或下拉菜单,支持单选或复选。

https://github.com/rushisangani/RSSelectionMenu/blob/master/Images/multi1.gif

演示视频

功能

  • 单选复选
  • 显示类似 展示推入弹出表单警报操作表 的显示风格菜单
  • 设置 UITableViewCell 类型如 基本副标题右侧细节
  • 设置 UITableViewCell 选择样式 勾选标记复选框
  • 从列表中搜索条目
  • 与自定义 UITableViewCell 一起使用
  • 与自定义模型类和结构体一起使用
  • 设置最大选择限制
  • 提供空数据集文本
  • 提供 所有自定义文本 的表头行
  • 可自定义设计 UINavigationBar 和 UISearchBar

7.1.2 更新了什么

  • 支持 Equatable 遵循
  • 在 Formsheet 展示中支持自定义大小
  • Swift 包管理器支持

已经在使用?迁移到7.1.2

  • 删除所有关于UniquePropertyDelegateuniquePropertyNamegetUniquePropertyName()的引用
  • 在模型类中符合Equatable规范(如有需要)

需求

iOS 9.0+ | Xcode 8.3+ | Swift 3.0+

安装

CocoaPods

pod 'RSSelectionMenu' or pod 'RSSelectionMenu', '~> 7.1.2'

Carthage

Carthage是一个去中心化的依赖管理工具,它会构建你的依赖并提供二进制框架。

你可以使用以下命令通过 Homebrew 安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 在 Xcode 项目中集成 RSSelectionMenu,请在 Cartfile 中指定它

github "rushisangani/RSSelectionMenu" ~> 7.1

然后按照以下步骤操作

  • 运行 carthage update 以构建框架。
  • 在目标构建设置中设置框架搜索路径:在 "Build Settings" 中选择 "Framework Search Paths": $(PROJECT_DIR)/Carthage/Build/iOS
  • Embedded Binaries 中添加 RSSelectionMenu.framework。
  • Linked Frameworks and Libraries 中添加 RSSelectionMenu.framework。

使用方法

简单选择列表

let simpleDataArray = ["Sachin", "Rahul", "Saurav", "Virat", "Suresh", "Ravindra", "Chris"]
var simpleSelectedArray = [String]()

// Show menu with datasource array - Default SelectionStyle = single
// Here you'll get cell configuration where you'll get array item for each index
// Cell configuration following parameters.
// 1. UITableViewCell   2. Item of type T   3. IndexPath

let selectionMenu = RSSelectionMenu(dataSource: simpleDataArray) { (cell, item, indexPath) in
    cell.textLabel?.text = item
}

// set default selected items when menu present on screen.
// here you'll get handler each time you select a row
// 1. Selected Item  2. Index of Selected Item  3. Selected or Deselected  4. All Selected Items

selectionMenu.setSelectedItems(items: simpleSelectedArray) { [weak self] (item, index, isSelected, selectedItems) in

    // update your existing array with updated selected items, so when menu show menu next time, updated items will be default selected.
    self?.simpleSelectedArray = selectedItems
}

// show as PresentationStyle = push
selectionMenu.show(style: .push, from: self)

多选列表

let selectionMenu = RSSelectionMenu(selectionStyle: .multiple, dataSource: simpleDataArray) { (cell, name, indexPath) in

    cell.textLabel?.text = name

    // customization
    // set image
    cell.imageView?.image = #imageLiteral(resourceName: "profile")
    cell.tintColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
}
  • 设置最大选择限制(可选)
selectionMenu.setSelectedItems(items: selectedDataArray, maxSelected: 3) { (item, selected, selectedItems) in
}
// or 
selectionMenu.maxSelectionLimit = 3

单元格选择样式

selectionMenu.cellSelectionStyle = .tickmark
// or
selectionMenu.cellSelectionStyle = .checkbox

展示样式 - 表单样式、弹出框、警告、操作表

// show as formSheet
selectionMenu.show(style: .formSheet, from: self)


// show as popover
selectionMenu.show(style: .popover(sourceView: sourceView, size: nil), from: self) 

// or specify popover size
selectionMenu.show(style: .popover(sourceView: sender, size: CGSize(width: 200, height: 300)), from: self)


// show as alert
selectionMenu.show(style: .alert(title: "Select", action: nil, height: nil), from: self)

// or specify alert button title
selectionMenu.show(style: .alert(title: "Select", action: "Done", height: nil), from: self)


// show as actionsheet
selectionMenu.show(style: .actionSheet(title: nil, action: "Done", height: nil), from: self)

自动消失

防止单选时的自动消失

selectionMenu.dismissAutomatically = false

事件处理器

消失时

selectionMenu.onDismiss = { [weak self] selectedItems in
    self?.selectedDataArray = selectedItems
    
    // perform any operation once you get selected items
}

将要出现

selectionMenu.onWillAppear = {
    /// do something..
}

自定义

搜索栏

  • 当用户开始在搜索栏中输入时,将通过处理器进行通知。
// show searchbar
selectionMenu.showSearchBar { [weak self] (searchText) -> ([String]) in

  // return filtered array based on any condition
  // here let's return array where name starts with specified search text

  return self?.dataArray.filter({ $0.lowercased().hasPrefix(searchText.lowercased()) }) ?? []
}

单元格样式 - 右侧详情或副标题

let selectionMenu = RSSelectionMenu(selectionType: .single, dataSource: dataArray, cellType: .rightDetail) { (cell, item, indexPath) in

    // here you can set any text from object
    // let's set firstname in title and lastname as right detail

    let firstName = item(separatedBy: " ").first
    let lastName = item.components(separatedBy: " ").last

    cell.textLabel?.text = firstName
    cell.detailTextLabel?.text = lastName
}

selectionMenu.setSelectedItems(items: selectedDataArray) { [weak self] (item, selected, selectedItems) in
    self?.selectedDataArray = selectedItems
}

// show as default
selectionMenu.show(from: self)

自定义单元格

  • 提供带有xib文件名和单元格标识符的自定义单元格。
let cellNibName = "CustomTableViewCell"
let cellIdentifier = "cell"

// create menu with multi selection and custom cell

let selectionMenu =  RSSelectionMenu(selectionStyle: .multiple, dataSource: customDataArray, cellType: .custom(nibName: cellNibName, cellIdentifier: cellIdentifier)) { (cell, person, indexPath) in

    // cast cell to your custom cell type
    let customCell = cell as! CustomTableViewCell

    // here you'll get specified model object
    // set data based on your need
    customCell.setData(person)
}

表头行 - 空、无、全部或自定义

// To show first row as Empty, when dropdown as no value selected by default
// add first row as empty -> Allow empty selection

let isEmpty = (selectedDataArray.count == 0)
selectionMenu.addFirstRowAs(rowType: .empty, showSelected: isEmpty) { (text, selected) in

    /// do some stuff...
    if selected {
        print("Empty Option Selected")
    }
}

空数据字符串

// show message 'No data found'
menu.showEmptyDataLabel()

// or
menu.showEmptyDataLabel(text: "No players found")

DataSource - Equatable conformance

struct Employee: Equatable {
    
    let empId: Int?
    let name: String?
}

class Person: NSObject {

    let id: Int
    let firstName: String
    let lastName: String

    init(id: Int, firstName: String, lastName: String) {
        self.id = id
        self.firstName = firstName
        self.lastName = lastName
    }
}

UI 自定义

导航栏

  • 设置标题、按钮标题、 tint 颜色和标题颜色
// set navigation bar title and attributes
selectionMenu.setNavigationBar(title: "Select Player", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white], barTintColor: #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1), tintColor: UIColor.white)

// right barbutton title - Default is 'Done'
selectionMenu.rightBarButtonTitle = "Submit"

// left barbutton title - Default is 'Cancel'
selectionMenu.leftBarButtonTitle = "Close"

搜索栏

  • 设置占位符、 tint 颜色
// show searchbar with placeholder and barTintColor
selectionMenu.showSearchBar(withPlaceHolder: "Search Player", barTintColor: UIColor.lightGray.withAlphaComponent(0.2)) { [weak self] (searchText) -> ([String]) in

    return self?.dataArray.filter({ $0.lowercased().starts(with: searchText.lowercased()) }) ?? []
}

示例

查看更多详情请参考 示例

许可

RSSelectionMenu 遵循 MIT 许可。有关详情,请参阅 LICENSE