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