Reputation: 302
I cannot seem to logically figure out how to have a separate array for each grouped section that would each show different data in the detailed view of a navigation controller. For each group, there is different data, but there the second view currently displays data in a UILabel from one array and starts at the beginning of the array for each group. Please let me know if you need more information.
- (void)viewDidLoad
{
[super viewDidLoad];
//Keeping of Business Names for Detailed View
addressBook=@[@"Clinic 1 Business",@"Clinic 2 Business",@"Clinic 3 Business"];
//holding of clinic names per county
adams=@[@"Clinic 1 Business Listing",@"Clinic 2 Business Listing",@"eeeee"];
allegheny=@[@"Clinic 3 Business Listing",@"Clinic 4 Business Listing"];
armstrong=@[@"Clinic 5 Business Listing",@"Clinic 6 Business Listing",@"Clinic 7 Business Listing"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
NSLog(@"this is the content of addressBook: \n %@", addressBook);
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return addressBook.count;
if(section==0)
{
return [adams count];
}
else if(section==1)
{
return [allegheny count];
}
else if (section==2)
{
return [armstrong count];
}
}
/*
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"Rec", @"A", @"B", nil ];
}
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"abCell";
addressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(indexPath.section==0)
{
cell.addressBookLbl.text=[adams objectAtIndex:indexPath.row];
}
else if(indexPath.section==1)
{
cell.addressBookLbl.text=[allegheny objectAtIndex:indexPath.row];
}
else if (indexPath.section==2)
{
cell.addressBookLbl.text=[armstrong objectAtIndex:indexPath.row];
}
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier]isEqualToString:@"addressBookDetails"]) {
addressBookDetailsViewController *addressDetailsController = [segue destinationViewController];
NSIndexPath *abIndexPath = [self.tableView indexPathForSelectedRow];
int row = [abIndexPath row];
addressDetailsController.addressDetail=@[addressBook[row]];
NSLog(@"this is the content of addressBook: \n %@", addressBook);
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return @"Adams County";
}
else if(section==1)
{
return @"Allegheny County";
}
else if(section==2)
{
return @"Armstrong County";
}
}
Upvotes: 0
Views: 229
Reputation:
The way that you would pass data along would be by using a segue, which is what you are doing. The thing that you are not doing correctly is in the prepareForSegue
method. Just to simplify things, use the didSelectRowAtIndexPath
method, so you aren't creating your own way of finding the selected cell.
To handle that, you will have to add a little to the didSelectRowAtIndexPath
. Here is what you need to do:
The:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
method that you have, should look like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"addressBookDetails"]) {
addressBookDetailsViewController *addressDetailsController = [segue destinationViewController];
addressDetailsController.addressDetail = selectedAddressBook;
//selectedAddressBook is an array that you don't have yet, but will create in the next step
//Make sure that the addressDetail in the DetailViewController is an Array, and not something else
}
}
Now you want to create the array selectedAddressBook
. So in the header (we're still in the MasterViewController):
@interface //blah blah blah
{
NSArray *selectedAddressBook;
}
OK, now back to the implementation file (still MasterViewController). Now you are going to use the didSelectRowAtIndexPath
method (I'll give an explanation on the importance of this at the end).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//some animation to fade away the blue on selection
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//here you will populate the array that you created
selectedAddressBook = (NSArray *)addressBook[indexPath.row];
//now you perform your segue
[self performSegueWithIdentifier:@"addressBookDetails" sender:self];
}
If you try and run it now, it will probably crash. Remove the existing segue. Just delete it. Create a new segue between the MasterViewController itself and the DetailViewController. Select whatever type you want, but you should probably use the Replace
segue. Give that segue the same identifier as the one you just deleted.
Now, in the DetailViewController .m file put an NSLog to watch for data in the viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Did the array get passed along? Let's see :\n %@", addressDetail);
//addressDetail should be the name of the array in this VC
}
If all went well, then the Log should show the details ONLY for the row that was selected. Now in the DetailViewController, you can access the data from the array and display it however you want to.
The reason you need to yu the didSelectRowAtIndexPath
method when using a tableView, is really just because is makes life simple. This method recognizes the row that was selected, by using NSIndexPath. Which does away with the way you were watching it in your prepareForSegue
method. It tells the delegate that the specified row is now selected. Using that, you can selectively perform actions on cell taps.
Upvotes: 1
Reputation: 4111
I'm a little confused by what you are trying to do with the addressBook
array you have above. The rest, looks like you should use a dictionary for. Are you familiar with dictionaries? You could set the key to the county and the value to array for that county. Then when you are asking for numberOfSectionsInTableView
you return [myDictionary allKeys].count
Let me know if that makes sense. If not, I'll elaborate further. Also, describe what the purpose of addressBook
is.
Upvotes: 0