UIPickerView depending on UITableView

99封情书 提交于 2019-12-08 03:11:36

问题


I don't have much experience in programming, so here is my question:

I'm trying to write a converter app. At the end, you can enter a number. Then I have a two component UIPickerView. With the first component, you select the input format (e.x. °Celcius). With the second component, you select the output format (e.x. °Fahrenheit), in order to convert °Celcius into °Fahrenheit.

But I want to add more units (weight, energy etc.). I want them to display on a UITableView. If I select a certain cell in the UITableView, the UIPickerView has to update its delegate. More precisely, the array in which I have the names of the units (e.x. eV, Joule, cal, erg, etc...) must be somehow implemented in the detailViewController (where I have my UIPickerView).

Here's my code:

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:


If I am not wrong you are looking for something like this.

To have it so you must implement UIPickerView methods as follows

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

In UITableView Delegate you can reload the picker View as follows.

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

To create the items for Table which has components as well units , you can use .plist file as follows

To help you with UITableView Methods I am adding the required code.

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

CHANGES If you want it to show as displayed below, minor tweaks to my code is required.

@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

Header File is quite simple too.

#import <UIKit/UIKit.h>

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

Please find the Demo project here



来源:https://stackoverflow.com/questions/16648459/uipickerview-depending-on-uitableview

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