iOS學習 - UIPickerView

UIPickerView使用:




效果所示

1粥惧、單個PickerView的使用



#pragma mark ===== UIPickerView

-(UIPickerView *)pickerView

{

if (!_pickerView) {

_pickerView=[[UIPickerView alloc]init];

_pickerView.frame =CGRectMake(0, YHScreenHeight-300, YHScreenWith, 300);

_pickerView.backgroundColor=[UIColor whiteColor];

_pickerView.delegate = self;

_pickerView.dataSource = self;

_pickerView.showsSelectionIndicator = YES;

}

return _pickerView;

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 1;

}

#pragma mark =====

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return self.ProvinceArray.count;

}

#pragma mark ===== 顯示內(nèi)容

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return self.ProvinceArray[row];

}

#pragma mark ===== 每列寬度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

return YHScreenWith;

}

#pragma mark ===== 行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

return 40;

}

#pragma mark ===== 返回選中的行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

NSString *str=self.ProvinceArray[row]; //輸出的結(jié)果

}

#pragma mark ===== 自定義顯示的view

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel* pickerLabel = (UILabel*)view;

if (!pickerLabel){

pickerLabel = [[UILabel alloc] init];

pickerLabel.adjustsFontSizeToFitWidth = YES;

pickerLabel.textColor=[UIColor redColor];

pickerLabel.textAlignment = NSTextAlignmentCenter;

[pickerLabel setBackgroundColor:[UIColor clearColor]];

[pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];

}

//設置分割線的顏色

for(UIView *singleLine in pickerView.subviews)

{

if (singleLine.frame.size.height < 1)

{

singleLine.backgroundColor = [UIColor greenColor];

}

}

pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];

return pickerLabel;

}

效果圖




2 ,多個PickerView的使用


#pragma mark ===== UIPickerView

-(UIPickerView *)pickerView

{

if (!_pickerView) {

_pickerView=[[UIPickerView alloc]init];

_pickerView.backgroundColor=[UIColor whiteColor];

_pickerView.delegate = self;

_pickerView.dataSource = self;

_pickerView.showsSelectionIndicator = YES;

}

return _pickerView;

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 3;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

NSInteger integer=0;

if (component==0) {

integer=self.ProvinceArray.count;

}

else if (component==1)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

integer=model.city.count;

}

else if (component==2)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

CityDataModel *cityModel=model.city[self.selectRowWithCity];

integer=cityModel.District.count;

}

return integer;

}

#pragma mark ===== 顯示內(nèi)容

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

NSString *string =nil;

if (component==0) {

ProvinceDataModel * model= self.ProvinceArray[row];

string=model.name;

}

else if (component==1)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

CityDataModel *cityModel=model.city[row];

string=cityModel.name;

}

else if (component==2)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

CityDataModel *cityModel=model.city[self.selectRowWithCity];

DistrictDataModel *TeanModel=cityModel.District[row];

string=TeanModel.name;

}

return string;

}

#pragma mark ===== 每列寬度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

return YHScreenWith/3;

}

#pragma mark ===== 行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

return 40;

}

#pragma mark ===== 返回選中的行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

if (component==0)

{

self.selectRowWithProvince=row;

self.selectRowWithCity=0;

[pickerView reloadComponent:1];

[pickerView selectRow:0 inComponent:1 animated:YES];

[pickerView reloadComponent:2];

[pickerView selectRow:0 inComponent:2 animated:YES];

}

else if (component==1)

{

self.selectRowWithCity=row;

[pickerView reloadComponent:2];

[pickerView selectRow:0 inComponent:2 animated:YES];

}

else

{

self.selectRowWithTown=row;

}

}

#pragma mark ===== 自定義顯示的view

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel* pickerLabel = (UILabel*)view;

if (!pickerLabel){

pickerLabel = [[UILabel alloc] init];

pickerLabel.adjustsFontSizeToFitWidth = YES;

pickerLabel.textColor=[UIColor redColor];

pickerLabel.textAlignment = NSTextAlignmentCenter;

[pickerLabel setBackgroundColor:[UIColor clearColor]];

[pickerLabel setFont:[UIFont boldSystemFontOfSize:14]];

}

//設置分割線的顏色

for(UIView *singleLine in pickerView.subviews)

{

if (singleLine.frame.size.height < 1)

{

singleLine.backgroundColor = [UIColor magentaColor];

}

}

pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];

return pickerLabel;

}?


效果圖


Dome 地址:?下載地址

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蚕钦,一起剝皮案震驚了整個濱河市腐碱,隨后出現(xiàn)的幾起案子实蔽,更是在濱河造成了極大的恐慌别垮,老刑警劉巖涯贞,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異星持,居然都是意外死亡抢埋,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來揪垄,“玉大人穷吮,你說我怎么就攤上這事〖⑴” “怎么了捡鱼?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長酷愧。 經(jīng)常有香客問我驾诈,道長,這世上最難降的妖魔是什么溶浴? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任乍迄,我火速辦了婚禮,結(jié)果婚禮上士败,老公的妹妹穿的比我還像新娘闯两。我一直安慰自己,他們只是感情好谅将,可當我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布漾狼。 她就那樣靜靜地躺著,像睡著了一般饥臂。 火紅的嫁衣襯著肌膚如雪逊躁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天擅笔,我揣著相機與錄音志衣,去河邊找鬼屯援。 笑死猛们,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的狞洋。 我是一名探鬼主播弯淘,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吉懊!你這毒婦竟也來了庐橙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤借嗽,失蹤者是張志新(化名)和其女友劉穎态鳖,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體恶导,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡浆竭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片邦泄。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡删窒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出顺囊,到底是詐尸還是另有隱情肌索,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布特碳,位于F島的核電站诚亚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏午乓。R本人自食惡果不足惜亡电,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望硅瞧。 院中可真熱鬧份乒,春花似錦、人聲如沸腕唧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枣接。三九已至颂暇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間但惶,已是汗流浹背耳鸯。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留膀曾,地道東北人县爬。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像添谊,于是被迫代替她去往敵國和親财喳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容