So I know that if you guys don’t like my question you are going to vote me down and reduce my reputation 🙁 but I need to learn and can’t find a answer so here I’m risking my reputation again.
Now to the point. All I’m trying to do is one View with two buttons and a table view under the buttons, this table view needs to display a different array depending witch button was click, in other words all I want it to do is if I click button1 display array1, if I click button2 display array2, simple! but I can’t get it to work.
Here is a sample of my code:
- (void)viewDidLoad
{
array1 = [[NSArray arrayWithObjects:
@"A",
@"B",
@"c",
@"D", nil] retain];
array2 = [[NSArray arrayWithObjects:
@"1",
@"2",
@"3",
@"4", nil] retain];
}
- (IBAction)btnPeriodicos:(id)sender {
displayArray = 1;
}
- (IBAction)btnRadios:(id)sender {
displayArray = 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (displayArray == 1) {
return [array1 count];
}
if (displayArray == 2) {
return [array2 count];
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blackColor];
if (menuNumberInt == 1) {
cell.textLabel.text = [[periodicoArray objectAtIndex:indexPath.row] retain];
}
if (menuNumberInt == 2) {
cell.textLabel.text = [[radioArray objectAtIndex:indexPath.row] retain];
}
return cell;
}
If I put instead of the if else statements either [array1 count] for the numberOfRowsInSection and the cell.textLabel.text = [[periodicoArray objectAtIndex:indexPath.row] retain]; the app loads with the table fill with that array so I guess the table works, but how can I make my buttons change the table cells?
any help is greatly appreciated.
Thanks
You can have an iVar variable for NSArray *currentArray. In viewDidLoad you can define your two arrays (as you currently do) but then assign currentArray to array1.
Then, change all the tableView callbacks to operate off of currentArray and get rid of the if (displayArray == x) checks.
in the (IBAction) button handlers, assign currentArray to the appropriate array and call [tableView reloadData]. that will clear the table and trigger all the tableView callbacks to happen again. since the currentArray changed, all the callback will pull data from the appropriate array.
Hope that helps.