AUPickerCell
表格单元格嵌入的选取视图。
要求
AUPickerCell需要Swift 5.0和Xcode 10.2。
安装
CocoaPods
您可以使用CocoaPods将AUPickerCell集成到项目中。
只需将以下行添加到您的Podfile中
pod "AUPickerCell"
然后在项目目录中运行pod update
Carthage
Carthage是一个去中心化的依赖性管理器,构建你的依赖并提供二进制框架。
您可以使用以下命令使用Homebrew安装Carthage:
brew update
brew install carthage
要使用Carthage将AUPickerCell集成到您的Xcode项目中,请将其指定在您的Cartfile中
github "azizuysal/AUPickerCell"
运行carthage update
以构建框架,并将构建好的AUPickerCell.framework
拖入您的Xcode项目中。
手动安装
您可以通过以下方式将AUPickerCell手动集成到您的项目中:将AUPickerCell.framework
拖放到Xcode中的“链接框架和库”部分,或者将AUPickerCell.swift
源文件复制到您的项目中。
使用方法
您可以将AUPickerCell用作UITableView中的UITableViewCell子类。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = AUPickerCell(type: .default, reuseIdentifier: "TableCell")
cell.values = ["One", "Two", "Three"]
cell.selectedRow = 1
cell.leftLabel.text = "Options"
return cell
}
之后,在您的
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let cell = tableView.cellForRow(at: indexPath) as? AUPickerCell {
return cell.height
}
return super.tableView(tableView, heightForRowAt: indexPath)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath) as? AUPickerCell {
cell.selectedInTableView(tableView)
}
}
上述示例产生了一个嵌入UIPickerView
的单元格,但您可以轻松地设置选择器的类型
为.date
,如下所示,以创建一个嵌入UIDatePicker
的单元单元格。
let cell = AUPickerCell(type: .date, reuseIdentifier: "PickerDateCell")
当用户交互时,单元格将自动更新右侧标签文本,以反映用户的选择。您还可以实现一个代理方法来通知用户的选择。
class MyViewController: UITableViewController, AUPickerCellDelegate {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = AUPickerCell(type: .default, reuseIdentifier: "PickerDefaultCell")
cell.delegate = self
...
}
}
func auPickerCell(_ cell: AUPickerCell, didPick row: Int, value: Any) {
self.pickedValue = value as! String
...
}
对于日期选择器的情况:
func auPickerCell(_ cell: AUPickerCell, didPick row: Int, value: Any) {
self.pickedDate = value as! Date
...
}
许可证
MIT许可证(MIT)