V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
banxi1988
V2EX  ›  iDev

分享一个封装适配器,简化 UIPickerView 的 80% 使用场景

  •  
  •   banxi1988 ·
    banxi1988 · 2015-06-03 23:21:27 +08:00 · 2249 次点击
    这是一个创建于 3275 天前的主题,其中的信息可能已经有所发展或是发生改变。

    NumberPickerAdapter 使用简介

    很大情况下,简单的对一些API进行封装就可以简化 80% 的使用场景.
    为什么说 80% ? 应该有一个法则中 28法则.

    NumberPickerAdapter的用法

    非常简单,使用如下:
    以一个月份选择 View 为例

    // 1)声明月份选择适配器
     let monthPickerAdapter = NumberPickerAdapter(range: 1...12)  
    
     // 2) 创建UIPickerView
       let monthPicker = UIPickerView(frame: CGRectZero)
    
    // 3) 设置数据源及委托
       monthPicker.dataSource = monthPickerAdapter
       monthPicker.delegate = monthPickerAdapter
    
    // 4) 使用闭包 (Block) 响应月份选择变更事件
     monthPickerAdapter.selectNumberBlock = { (month) in
          self.updateDayPickerWithMonth(month)
     }
    

    基本的使用就是这么简单.
    基本的简化逻辑就是,将 DataSource 和 Delegate整合在一起了
    事实上,对于 iOS 8上的 AlertController 也是这样的逻辑,
    简单的事件处理,提供一个Block 就解决了,不用再搞一个 Delegate.

    不用Delegate的,另一个好处是,代码都在一块. 这样维护,或者阅读起来比较方便吧.

    对于外观的定制,也提供发如下几个常用的定制属性,示例如下:

    adapter.selectedTitleColor = AppColors.colorPrimary
     adapter.titleColor = UIColor(white: 0.509, alpha: 1.0)
     adapter.titleFont = UIFont.systemFontOfSize(17)
    

    方便获得 UIPickerView 当前值的Cateogry

    UIPickerView 添加了一个 Category.
    以便在使用 NumberPickerAdapter 时,方便的获得所选的数字

    monthPicker.selectedNumber

    对应代码:

    var selectedNumber:Int{
            if let adapter = dataSource as? NumberPickerAdapter{
                return adapter.numberAtRow(selectedRowInComponent(0))
            }
            return 0
        }
    

    有时很重要的一点是,方便设置值

    由于 UIPickerView 实际设置当前值是通过,对应行来实现的.
    如果你的数字是不连续的.就可能会有用户设置的值不规则

    这个时候,可以试试 NumberPickerAdapter 提供的
    func bestRowOfNumber(number:Int) -> Int
    来根据某一个值,查找最匹配的行

    显示值的转换

    针对数字的显示, 有时你想数字显示为 "09" ,有前导0
    var numberFormat = "%d"
    默认情况下,只是将数字转换成字符串.
    如果需要设置显示为 "09" 可以,使用标准的格式化手段
    adapter.numberFormat = "%02d"

    这样,这方面的要求也解决了.

    当然,如果你有更复杂的格式化需求, 可以使用一个Block来实现.

    源代码

    如下:
    https://gist.github.com/banxi1988/c73fe61860ef63bd61ee.js

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4041 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 04:04 · PVG 12:04 · LAX 21:04 · JFK 00:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.