UIPickerView取决于UITableView

时间:2013-05-20 11:25:57

标签: ios uitableview uipickerview

我在编程方面没有太多经验,所以这是我的问题:

我正在尝试编写转换器应用。最后,您可以输入一个数字。然后我有一个双组件UIPickerView。使用第一个组件,您可以选择输入格式(e.x.°Celcius)。使用第二个组件,您可以选择输出格式(e.x.°Fahrenheit),以便将°Ccius转换为°华氏度。

但我想增加更多单位(重量,能量等)。我希望它们显示在UITableView上。如果我在UITableView中选择某个单元格,则UIPickerView必须更新其委托。更确切地说,我必须以detailViewController(我有UIPickerView的方式实现我拥有单位名称(ex eV,Joule,cal,erg等)的数组。 )。

这是我的代码:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong,nonatomic) IBOutlet UIPickerView *EinheitenPicker;
@property (weak,nonatomic) IBOutlet UITableView *EinheitenTableView;

@end

ViewController.m

#import "ViewController.h"


@interface ViewController ()
{
    NSArray *EinheitenNameArray;
}
@end

@implementation ViewController
@synthesize EinheitenPicker,EinheitenTableView;
- (void)viewDidLoad
{
    [super viewDidLoad];

    EinheitenNameArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AlleEinheiten" ofType:@"plist"]];

    [self.EinheitenTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];

    [self.EinheitenTableView.delegate tableView:self.EinheitenTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark UItableView Delegate
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return EinheitenNameArray.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"CellId";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text=[[EinheitenNameArray objectAtIndex:indexPath.row] objectForKey:@"Titel"];

    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //NSLog(@"Chosen:%@",self.EinheitenPicker);

    [self.storyboard instantiateViewControllerWithIdentifier:@"TestId"];

    [self.EinheitenPicker reloadAllComponents];
}

#pragma mark UIPickerView Methods

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

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSIndexPath *selectedIndexPath=[self.EinheitenTableView indexPathForSelectedRow];
    NSArray *units=[[EinheitenNameArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Einheiten"];

    if (component == 0) {
        return units.count;
    }

    return units.count;   
}

- (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSIndexPath *selectedIndexPath=[self.EinheitenTableView indexPathForSelectedRow];
    NSArray *units=[[EinheitenNameArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Einheiten"];
    return [units objectAtIndex:row];
}

@end

1 个答案:

答案 0 :(得分:3)

如果我没有错,你正在寻找类似的东西。

enter image description here

要拥有它,您必须按如下方式实现UIPickerView方法

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

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
    NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.section] objectForKey:@"units"];
    return units.count;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
    NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.section] objectForKey:@"units"];
    return [units objectAtIndex:row];
}

UITableView Delegate 中,您可以按如下方式重新加载选择器视图。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.unitPicker reloadAllComponents];
}

要为包含组件和单位的表创建项目,您可以使用.plist文件,如下所示

enter image description here

为了帮助您使用UITableView方法,我将添加所需的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    unitDataArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:<Name of plist File> ofType:@"plist"]];
    [self.unitsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self.unitsTableView.delegate tableView:self.unitsTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return unitDataArray.count;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *unitData=[unitDataArray objectAtIndex:section];
    return [[unitData objectForKey:@"units"] count];
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier=@"CellId";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=[[[unitDataArray objectAtIndex:indexPath.section] objectForKey:@"units"] objectAtIndex:indexPath.row];

    return cell;
}

-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSDictionary *unitData=[unitDataArray objectAtIndex:section];
    return [unitData objectForKey:@"title"];
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.unitPicker reloadAllComponents];
}

<强>更改 如果您希望它显示如下所示,则需要对我的代码进行微调。

enter image description here

@implementation ViewController
@synthesize unitPicker,unitsTableView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    unitDataArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AllUnits" ofType:@"plist"]];
    [self.unitsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self.unitsTableView.delegate tableView:self.unitsTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

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

#pragma mark UItableView Delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;//Changes 1
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return unitDataArray.count;//Changes 2
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier=@"CellId";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=[[unitDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];

    return cell;
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.unitPicker reloadAllComponents];
}

#pragma mark UIPicker View Methods

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

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
    NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.row] objectForKey:@"units"];//Changes 4
    return units.count;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
    NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.row] objectForKey:@"units"];//Changes 5
    return [units objectAtIndex:row];
}




@end

标题文件也很简单。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak,nonatomic) IBOutlet UIPickerView *unitPicker;
@property (weak,nonatomic) IBOutlet UITableView *unitsTableView;
@end

请找到Demo project here