Xcode控件使用筆記二:DatePicker、PickerView

DatePicker控件:

    UIDatePicker *picker = [[UIDatePicker alloc] init];
    picker.datePickerMode = UIDatePickerModeDate;
    picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

日期處理:

   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 設置日期格式(2010/10/10)
    formatter.dateFormat = @"yyyy/MM/dd";
    // 將NSDate轉成NSString
    NSString *dateStr = [formatter stringFromDate:date];

PickerView控件:

1、用到協議

@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

2、實現類別中的方法

#pragma mark -UIPickerView的數據源方法----設置UIPickerView中的列數和行數
#pragma mark -返回compinent列的行數
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return component==0?_oneCol.count:_twoCol.count;
}
#pragma mark -返回PickerView的compinent列數
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
#pragma mark -UIPickerView的代理方法
#pragma mark -返回第compinent列第row行顯示的字符串數據----在UIPickerView中放入數據
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return _oneCol[row];
    }else{
        return _twoCol[row];
    }

}
#pragma mark 選中默一行就會調用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==0) {
        NSString *left= _oneCol[row];
        int rightRow=[pickerView selectedRowInComponent:1];
        NSString *right=_twoCol[rightRow];
        NSLog(@"%@-%@",left,right);
    }else{
        NSString *right= _twoCol[row];
        int leftRow=[pickerView selectedRowInComponent:0];
        NSString *left=_oneCol[leftRow];
        NSLog(@"%@-%@",left,right);
    }
}

顯示覆雜內容時:實現組裝數據時viewForRow

#pragma mark 返回第component列第row行數需要顯示的UIView

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章