iOS基礎-UIKit架構-進階視圖-UIPickerView-執行個體3:國家選擇(圖片)

來源:互聯網
上載者:User

標籤:

說明:事先已經準備好了一個NJCountry類,裡面聲明了2個屬性name和icon,並
建立和實現了快速建立類的動態方法和靜態方法。直接匯入即可。
0.建立一個plist檔案,Root為Array,裡面為字典(字典裡為國家和國旗)
1.載入這個plist檔案
1>聲明數組屬性
@property(nonatomic,strong)NSArray *countries;
2>懶載入(在實現檔案最後面)
#pragma mark - 懶載入
-(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;
}
2.將國旗圖片拖到支援檔案中。
3.

0>建立一個xib檔案NJCountryView.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>匯入標頭檔並重寫set方法(在這個方法裡賦值)
-(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即可。

iOS基礎-UIKit架構-進階視圖-UIPickerView-執行個體3:國家選擇(圖片)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.