iOS UIPickerView的簡單實用

來源:互聯網
上載者:User

標籤:

      其實UIPickerView和UITableview的實現方法中,大致是相同的,就連作用都是一樣的,只是運用在了不同的效果上而已,那麼下面就記錄幾個方法就可以實現UIPickerView的效果:

1、設定UIPickerView的列

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

2、設定UIPickerView 的行

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

3、設定UIPickerView的行高

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

4、設定UIPickerView的文本資訊

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

5、UIPickerView的點擊事件

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

      通過以上的5個步驟,就能實現一個滾動的效果。但是呢,這裡有一個問題,就是不能像UITableView中的Cell方法一樣去設定[MoreCell.textLabel setFont:[UIFont systemFontOfSize:15.0f]];的樣式或者字型大小,那麼這裡就涉及到了UIPickerView中的另外一個實現方法:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{       UILabel *lalTitle=(UILabel *)view;    if (!lalTitle) {        lalTitle=[[UILabel alloc] init];        lalTitle.minimumScaleFactor=8;//設定最小字型,與minimumFontSize相同,minimumFontSize在IOS 6後不能使用。        lalTitle.adjustsFontSizeToFitWidth=YES;//設定字型大小是否適應lalbel寬度        lalTitle.textAlignment=NSTextAlignmentCenter;//文字置中顯示        [lalTitle setTextColor:[UIColor blackColor]];        [lalTitle setFont:[UIFont systemFontOfSize:17.0f]];    }    lalTitle.text=[self pickerView:pickerView titleForRow:row forComponent:component];    return lalTitle;   }

  

    到此,UIPickerView的簡單實用就實現了!!!!

 

iOS UIPickerView的簡單實用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.