Reputation: 576
I'm working on an application in which I have a UITableView, two arrays and one UISegmentedControl. Now I need to when the UISegmentedControl value is 0 loaded in the UITableView data from the array one, and when UISegmentedControl has value 1 loaded data from array two. Simply, I need to switch the data to be loaded into UITableView from arrays. I tried to use a bool, but it does not work, I also think that it's not ideal.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"],nil ];
allTableData2 = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"],nil ];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
long rowCount;
if(self.isSwich == false)
rowCount = allTableData.count;
else
rowCount = allTableData2.count;
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
Food* food;
if(self.isSwitch == false)
{
food = [allTableData objectAtIndex:indexPath.row];
}
else
{
food = [allTableData2 objectAtIndex:indexPath.row];
}
cell.textLabel.text = food.name;
cell.detailTextLabel.text = food.description;
return cell;
}
-(IBAction)switchdata:(id)sender
{
if(self.myswitcher.selectedSegmentIndex == 0)
{
isSwitch = FALSE;
}
else
{
isSwitch = TRUE;
}
}
Upvotes: 1
Views: 238
Reputation: 726509
You are missing a call to reloadData
in the witchdata:
implementation. Adding this call will fix the problem.
I also think that it's not ideal.
That's right. Rather than storing a flag that says which array to use for your data source, store the array itself. This would eliminate all the if
s on the isSwitch
property:
// Declare this as an instance variable, and use it
// in your data source methods.
NSArray *theSource;
- (void)viewDidLoad {
[super viewDidLoad];
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"],nil ];
allTableData2 = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"], nil];
theSource = allTableData;
}
-(IBAction)switchdata:(id)sender {
if(self.myswitcher.selectedSegmentIndex == 0) {
theSource = allTableData;
} else {
theSource = allTableData2;
}
[myTable reloadData];
}
Alternatively you could make isSwitch
an integer, and use it as an index into an array that has your allTableData
at index zero and allTableData2
at index one:
NSArray *sources;
int sourceIndex;
// Use sources[sourceIndex] as the current source
- (void)viewDidLoad {
[super viewDidLoad];
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"], nil];
allTableData2 = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"],nil ];
sources = @[allTableData, allTableData2];
sourceIndex = 0;
}
-(IBAction)switchdata:(id)sender {
sourceIndex = self.myswitcher.selectedSegmentIndex;
[myTable reloadData];
}
Upvotes: 2
Reputation: 181
Firstly, in your switchData: method - use self.switch to access its setters,getters.
Secondly, Rather than using a bool value to track segment value, use the direct segment value. Replace your self.switch value with self.switcher.selectedSegmentIndex in the table view delegate,datasource methods.
Finally, reload table view in your switchData: method.
Upvotes: 1