Jon
Jon

Reputation: 4732

Need Help Using Delegate To Change UITableView

I have setup a delegate method, so that when user selects cell from VC2, it updates the header text of VC1 then pops back to it.

Now I need to add some code that will let me change the array that loads the UITableView in VC1, what is the easiest way to do this?

In VC 1

- (void)didChooseValue:(NSString *)string 
{
    self.databaseName = string;
    NSLog(@"Database Name: %@", self.databaseName);
    [self.myTableView reloadData];
}

In VC 2 - didSelectRowAtIndex

if([delegate respondsToSelector:@selector(didChooseValue:)])
    {
        [delegate performSelector:@selector(didChooseValue:) withObject:myString];
    }

Upvotes: 0

Views: 121

Answers (1)

Akshay
Akshay

Reputation: 5765

You can add the code in this delegate method itself. Try something like:

- (void)didChooseValue:(NSString *)string 
{
    self.databaseName = string;
    NSLog(@"Database Name: %@", self.databaseName);
    NSMutableArray *carModels = nil;

    if([string isEqualToString:@"Mazda"])
        carModels = [[Database sharedDatabase] getMazdaModels]; 
    else if([string isEqualToString:@"Nissan"])
        carModels = [[Database sharedDatabase] getNissanModels]; //And so on depending on the number of models
    else
        carModels = [[Database sharedDatabase] getDefaultModels];

    self.carAray = carModels;
    [self.myTableView reloadData];
}

Upvotes: 1

Related Questions