如何为此选择器视图提供数据源 [英] How to give this picker view a datasource

查看:74
本文介绍了如何为此选择器视图提供数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我以编程方式添加一个pickerview

Here I am adding a pickerview programaticly

 - (void)viewDidLoad {
        [super viewDidLoad];
        CGRect pickerFrame = CGRectMake(0,280,321,200);

        UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure

        //Set up the picker view as you need it

        //Set up the display frame
        myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions

        //Add the picker to the view
        [self.view addSubview:myPickerView];
    }

但是现在我需要让它显示内容并以某种方式找出它的时间变化以及变更的价值。我该怎么做呢?

But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?

推荐答案

在.h文件中放置此代码

in .h file place this code

@interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

将数据源和委托分配给选择器

assign the datasource and delegate to the picker

// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;

使用以下委托和数据方法

use the following delegate and datasouce methods

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

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 200;
}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *returnStr = @"";
    if (pickerView == myPickerView)
    {       
        returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:@"nodeContent"];
    }

    return returnStr;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == myPickerView)
    {
        return [levelPickerViewArray count];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

这篇关于如何为此选择器视图提供数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆