How to give this picker view a datasource

南笙酒味 提交于 2019-12-05 10:38:29
pradeepa

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;
}

You need to create a class that implements the UIPickerViewDataSource protocol and assign an instance of it to myPickerView.dataSource.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!