Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2093251
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类:

2010-04-24 12:50:10

  1. Introduction
    Picker can be configured to display one dail or many. By defults, pickers display lists of text, but they can also be made to display images.
  2. Types of Pickers
    • UIDatePicker
    • UIPickerView
  3. Delegates & Datasource
    (The controller view containing date piciker need not conform to UIPickerViewDelegate and UIPickerViewDataSource)
    • UIPickerViewDelegate
      • Set the visible data of current row
        pickerView:titleForRow:forComponent:
      • ...
    • UIPickerViewDataSource
      • Set component count (Column count)
        numberOfComponentsInPickerView:
      • Set the count of rows of current component
        pickerView:numberOfRowsInComponent:
  4. Dependent Component
    The data in some components depends on other component. For example, picker view contains 2 components, the first component is used to display proviences name, and the second component is used to display cities under the selected provience.
    1. Use NSDictionary to hold data
      #define COMPONENT_PROVIENCE = 0;
      #defien COMPONENT_CITY = 1;

      NSDictionary *pProvienceCity;
      NSArray *pProviences;
      NSArray *pCities;
      UIPickerView *pPickerView;
    2. Override pickerView:didSelectRow:inComponent:, then set the correct data array, finally, call reloadComponent: to reset
      - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
      {
          if (COMPONENT_PROVIENCE == component)
          {
              NSString *pProvience = [pProviences objectAtIndex:row]:
              self.pCities = [pProvienceCity objectForKey:pProvience];
              [pPickerView selectRow:0 inComponent:COMPONENT_CITY animated:YES];
              [pPickerView reloadComponent:COMPONENT_CITY];
          }
      }
    3. ....
  5. Sample Code
    1. Return data in selected row
      NSArray *pDrrayData;
      IBOutlet UIPcikerView *pPickerView;
      ...
      //Get the selected data in the first column, if you want to get //data in the third column, just replace 0 with 2
      NSInteger row = [pPickerView selectedRowInComponent:0];
      NSString *pData = pDrrayData[row];
    2. Set data to picker
      Suppose defined following fields:
      #define COMPONENT_NAME = 0;
      #defien COMPONENT_NUM = 1;

      NSArray *pDataName;
      NSArray *pDataNum;
      IBOutlet *pPickerView;


      - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
      {
          if (COMPONENT_NAME == component)
          {
              return [pDataName objectAtIndex:row];
          }
          else
          {
              return [pDataNum objectAtIndex:row];
          }
      }
    3. ...
  6. ...

Reference
  1. Beginning iPhone Development: Exploring the iPhone SDK
    Chapter: Application Settings and User Defaults
    Page: P139~183
阅读(906) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~