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:


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