Search code examples
uitableviewnsarrayuipickerview

having trouble using pickerview selection to populate tableview


what i am trying to do is use the selection from a pickerview to populate a tableview on another view controller. The user makes a selection on the picker view then taps a button to go to the next view controller which displays different arrays in a tableview depending on the pickerview selection.

I am quite new to coding so i don't know if my code is even remotely close to being correct. Anyway heres what I've tried:

@interface HomeViewController ()

@end

@implementation HomeViewController{

}

@synthesize tableview;

- (void)viewDidLoad
{
[super viewDidLoad];

_pickerarray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
_array1 = [[NSArray alloc] initWithObjects:@"1980",@"1981",@"1982", nil];
    _array2 = [[NSArray alloc] initWithObjects:@"1983",@"1984",@"1985", nil];
    _array3 = [[NSArray alloc] initWithObjects:@"1986",@"1987",@"1988", nil];

}

- (void)didReceiveMemoryWarning
{
[self setPicker:nil];
[super didReceiveMemoryWarning];
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerarray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_pickerarray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (row) {
    case 0:

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_array1 count];
    }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"cell";
        UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [_array1 objectAtIndex:indexPath.row];
        return cell;
    }

        break;
        case 1:
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_array2 count];

    }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"cell";
        UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [_array2 objectAtIndex:indexPath.row];
        return cell;
    }
        break;
        case 2:
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_array3 count];

    }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"cell";
        UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [_array3 objectAtIndex:indexPath.row];
        return cell;
    }

    default:
        break;
}
}

@end

On the line of code

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

I get a semantic issue stating " Use of undeclared identifier 'tableview'"

I would really appreciate any feedback, as I said I am quite new and i don't even know if I'm going about this the correct way so if anyone could steer me in the right direction that would be great!


Solution

  • As a novice I'd recommend you use a tableview framework such as the free Sensible TableView framework. These frameworks will enable to easily achieve such tasks almost out of box. Saves me tons of time in my projects.