UIPickerView
- 在開發過程中用到這個控件的機會比較小,常見的就是收獲地址之類的,或者說選擇性別,選擇生日之類的,用的比較少。
- 研究的過程中就會發現,
UIPickerView
的使用方法和UITableView
很像
使用步驟
- 創建一個UIPickerView控件
- 設置UIPickerView的delegate和dataSource
- 實現dataSource方法
- 實現delegate方法
基礎方法
// 設置代理
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
#pragma mark - UIPickerViewDataSource
/**
* 顯示多少列
*/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
}
/**
* 第component列顯示多少行
*/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
}
#pragma mark - UIPickerViewDelegate
/**
* 第component列row行顯示的文本
*/
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
}
/**
* 第component列的寬度
*/
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
}
/**
* 第component列的高度
*/
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
}
/**
* 第component列row行顯示的文本(富文本)
*/
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
}
/**
* 第component列row行顯示的view(優先級大于文本)
*/
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
}
/**
* 選中第component列的第row行
* 注意:這個方法必須用戶主動拖動pickerView,才會調用
*/
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
//設置某一列的某一行選中
//不會觸發pickerView:didSelectRow:inComponent:方法
[self.pickerView selectRow:index inComponent:i animated:YES];
聯動注意點
- 用到聯動的地方一般是地區選擇,選中省份的時候要改變相應的市區
- 這個時候最容易出現的問題就是當兩列都在滾動的時候會出現數組越界的問題
- 解決辦法就是當第一列省份停下來的時候要記錄下選中的編號,刷新市區的時候需要渠道特定的數據,不能一直變
//滾動停下來的時候調用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。