UIPickerView 自定义

#import "UIPickerViewController.h"

@interface UIPickerViewController ()

@end

@implementation UIPickerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [listArray release];
    [super dealloc];
}

- (void)loadView
{
    [super loadView];
    
    // DatePicker
//    UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
//    datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:- 60 * 60 * 24 * 365 * 10];
//    datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 365 * 10];
//    datePicker.datePickerMode = UIDatePickerModeDate;
//    [self.view addSubview:datePicker];
//    [datePicker release];
    
    //pickerview
    listArray = [[NSMutableArray alloc]init];
    [listArray addObject:[UIFont familyNames]];
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blackColor],[UIColor grayColor],[UIColor greenColor],[UIColor whiteColor],nil];
    [listArray addObject:colors];
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (int i = 10; i <= 50; i ++){
        [array addObject:[NSNumber numberWithInt:i]];
    }
    [listArray addObject:array];
    [array release];
    
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];
    [pickerView release];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 330, 320, 100)];
    label.text = @"中华民族";
    label.tag = 100;
    [self.view addSubview:label];
    [label release];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return [listArray count];
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [[listArray objectAtIndex:component] count];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0){
        return 160;
    }else if (component == 1){
        return 80;
    }else{
        return 60;
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *lable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, [self pickerView:pickerView widthForComponent:component], [self pickerView:pickerView rowHeightForComponent:component])] autorelease];
    
    if (component == 0){
        lable.text = [[listArray objectAtIndex:component] objectAtIndex:row];
    }else if (component == 1){
        lable.backgroundColor = [[listArray objectAtIndex:component] objectAtIndex:row];
    }else{
        lable.text = [NSString stringWithFormat:@"%@",[[listArray objectAtIndex:component] objectAtIndex:row]];
    }
    return  lable;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *label = (UILabel *)[self.view viewWithTag:100];
    NSString *fontname = [[listArray objectAtIndex:0] objectAtIndex:[pickerView selectedRowInComponent:0]];
    UIColor *color = [[listArray objectAtIndex:1] objectAtIndex:[pickerView selectedRowInComponent:1]];
    NSNumber *s = [[listArray objectAtIndex:2] objectAtIndex:[pickerView selectedRowInComponent:2]];
    label.backgroundColor = color;
    label.font = [UIFont fontWithName:fontname size:[s integerValue]];
   
}

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    return @"ggg";
//}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义一个 UIPickerView,你需要遵循 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,并实现其中的方法。 首先,你需要创建一个 UIPickerView 实例,并设置其数据源和代理为当前视图控制器。 ```swift class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { let pickerView = UIPickerView() override func viewDidLoad() { super.viewDidLoad() pickerView.dataSource = self pickerView.delegate = self view.addSubview(pickerView) } } ``` 接着,你需要实现 UIPickerViewDataSource 协议中的两个必须方法:numberOfComponents(in:) 和 numberOfRows(inComponent:)。前者用于返回选择器中的列数,后者用于返回每列中的行数。 ```swift func numberOfComponents(in pickerView: UIPickerView) -> Int { return 2 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return 10 } ``` 这里我们设置了两列,每列都有 10 行。 然后,你需要实现 UIPickerViewDelegate 协议中的方法来自定义选择器的样式和行为。比如,你可以自定义每一行的标题和宽度,以及选择器的动画效果。 ```swift func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return "Row \(row)" } func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return 100 } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { print("Selected row \(row) in component \(component)") } ``` 这里我们简单地返回了每一行的标题为 "Row \(row)",每列的宽度为 100,当选择器的行被选中时,会在控制台输出相应的信息。 最后,你需要将 UIPickerView 添加到你的视图中,并设置它的位置和大小。 ```swift pickerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200) ``` 这里我们将选择器的宽度设置为与视图宽度相同,高度为 200,然后将其添加到视图的顶部。 这样,你就可以自定义一个 UIPickerView 了!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值