zoukankan      html  css  js  c++  java
  • iOS基础-UIKit框架-高级视图-UIPickerView-实例3:国家选择(图片)

    说明:事先已经准备好了一个NJCountry类,里面声明了2个属性name和icon,并创建和实现了快速创建对象的动态方法和静态方法。直接导入即可。
    创建一个plist文件,Root为Array,里面为字典(字典里为国家和国旗)
    加载这个plist文件

    @property(nonatomic,strong)NSArray *countries;
    -(NSArray *)countries
    {
      if(_countries == nil){
      // 1.加载plist文件内容
      NSArray *dictArray = [NSArray arrayWithContentsOfFile:[NSBundle  mainBundle] pathForResource:@"flags.plist" ofType:nil]];
      //2.创建数组保存转换后的模型
      NSMutableArray *models = [NSMutableArray arrayWithCapacity: dictArray.count];
      //3.字典转模型
      for(NSDcitionary *dict in dictArray){
      NJCountry *flag = [NJCountry countryWithDictionary:dict];
      [models addObject:flag];
      }
      _countries = [models copy];
      }
       return _countries;
    }

    1.

    0>新建一个xib文件,拖一个UIView和一个Label,新建一个类NJCountryView来管理它。将xib文件的View的class属性设置为NJCountryView
    1>在类扩展中将标签和View连到NJCountryView这个类。(属性)
    2>在NJCountryView.h文件中将数据模型声明为属性,并提供类方法(封装xib)

    @class NJCountry;
    //数据模型
    @property(nonatomic,strong)NJCountry *country;
    + (insancetype)countryView;

    3>导入头文件并重写setter方法(在这个方法里赋值)

    -(void)setCountry:(NJCountry *)country
    {
    _country = country;
    // 1.设置国家名称
    self.nameLabel.text = _country.name;
    // 2.设置国旗
    self.iconView.image = [UIImage imageNamed:_country.icon]; 
    }
    4>实现类方法(用来快速创建自定义View)
    + (insancetype)countryView{
       return [[[NSBundle mainBundle] loadNibNamed:@"NJCountryView"  owner:nil options“nil] firstObject];
    }

    4.拖一个Picker View控件,设置控制器和代理,遵从协议并实现方法

    #pragma mark - 数据源方法
    //告诉系统有多少列
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *) pickerView
    {
        return 1;
    }
    
    //告诉系统有多少行
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponents:(NSInteger)component
    {
        return self.countries.count;
    }
    
    #pragma mark - 代理方法
    //告诉系统每一行显示什么内容
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponents:(NSInteger)component
    {
         return [self.countries[row] name];
    } 
    //告诉系统每一行显示什么视图
    //当一个view进入视野范围内的时候调用
    //当系统调用该方法的时候会自动传入可重用的view
    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger) row forComponents:(NSInteger)component resuingView :(UIView)view
    
    {
    //有默认的宽高
    //1.创建自定义View 
    NJCountryView *countryView = (NJCountryView *)view;
    if(countryView == nil){
    countryView = [NJCountryView countryView];
    }
    //2.赋值模型数据
    countryView.country = self.countries[row];
    
    //3.返回自定义view
    return countryView;
    }

    错误做法:直接在控制器返回高度

    //告诉系统每一行的高度
    -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponents:(NSInteger)component 
    {
        return 44;
    }

    //注意:直接返回高度的话封装性不好,自己多高应该自己最清楚。
    正确做法:
    在NJCountryView中提供一个方法返回高度

    +(CGFloat)rowHeight
    {
        return 54;
    }

    然后在控制器中调用这个方法

    //告诉系统每一行的高度
    -(CGFloat)pickerView:(UIPickerView *)pickerView  rowHeightForComponents:(NSInteger)component
    {
         return [NJCountryView rowHeight];
    }

    好处:以后需要修改高度不需要修改控制器,直接修改管理它的类NJCountryView即可。

  • 相关阅读:
    Using X++ code direct execute to sql statement
    SysMultiTableLookup class
    Using X++ Modify Table and class name From AOT Project
    identify multiple selected records
    简单实用SQL脚本(转)
    FTK应用程序编程接口(API)手册1
    《那些年啊,那些事——一个程序员的奋斗史》——28
    ftk的python binding
    《那些年啊,那些事——一个程序员的奋斗史》——27
    好久没来,CSDN都改版了
  • 原文地址:https://www.cnblogs.com/marshall-yin/p/4722309.html
Copyright © 2011-2022 走看看