Question

I have a plist with contacts: the root is an array, items 0-150 are dictionaries, each dictionary is a single contact with a "name", "number", and "email" string.

The code below sorts the contacts alphabetically into sections based upon the "name" string. Then uses the inner array to populate the cells for each section. I then pass the name from the inner array to my detail view.

However, I can not figure out how to pass the correct number and email for each contact into the detail view. I've been working on this issue for a long while and can not find a solution.

@interface ContactsViewController ()
-(void)configureSectionData;
@end

@implementation ContactsViewController
@synthesize tableData;
@synthesize collation;
@synthesize outerArray;
@synthesize indexTitlesArray, namesDictionary;

- (void)didReceiveMemoryWarning 
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - Table view methods

-(void)configureSectionData {

NSUInteger sectionTitlesCount = [collation.sectionTitles count];

self.outerArray = [NSMutableArray arrayWithCapacity:sectionTitlesCount];

for (NSUInteger index = 0; index < sectionTitlesCount; index++) {

    NSMutableArray *array = [NSMutableArray array];

    [self.outerArray addObject:array];

}

for (NSString *nameString in tableData)
{
NSInteger sectionNumber = [collation sectionForObject:nameString collationStringSelector:@selector(lowercaseString)];
NSMutableArray *sectionNames = [outerArray objectAtIndex:sectionNumber];
[sectionNames addObject:nameString];
}

for (NSUInteger index = 0; index < sectionTitlesCount; index++) {

    NSMutableArray *namesForSection = [outerArray objectAtIndex:index];

    NSArray *sortedNamesForSection = [collation sortedArrayFromArray:namesForSection collationStringSelector:@selector(lowercaseString)];

    [self.outerArray replaceObjectAtIndex:index withObject:sortedNamesForSection];

}

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.collation.sectionTitles count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

NSString *theLetter  = [self.collation.sectionTitles objectAtIndex:section];

if (![theLetter isEqualToString:@"#"]) {
    NSString *titleString = [NSString stringWithFormat:@"%@", theLetter];
    return titleString;
}

return nil;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.collation.sectionTitles;
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}

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

NSArray *innerArray = [self.outerArray objectAtIndex:section];
return [innerArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"cellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

// Get the inner array for this section
NSArray *innerArray = [self.outerArray objectAtIndex:indexPath.section];

// Get the name from the inner array
NSString *theName = [innerArray objectAtIndex:indexPath.row];

cell.textLabel.text = theName;

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainiPhoneStoryboard" bundle:nil];
DetailViewController *detailView = (DetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

// Get the inner array for this section
NSArray *innerArray = [self.outerArray objectAtIndex:indexPath.section];

// Get the name from the inner array
NSString *tmpname = [innerArray objectAtIndex:indexPath.row];
detailView.lblname = tmpname;

[self presentViewController:detailView animated:YES completion:nil];

}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.namesDictionary = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle]   pathForResource:@"contacts" ofType:@"plist"]];

self.tableData = [namesDictionary valueForKey:@"name"];

self.collation = [UILocalizedIndexedCollation currentCollation];

[self configureSectionData];

}
Was it helpful?

Solution

Since you're populating your table from an array of just the names from your plist, you'll have to search that array using the name to find the dictionary that it belongs to, so you can pass that to the detail view controller (you would need to create a property in your detail view controller, passedInDictionary in my example):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainiPhoneStoryboard" bundle:nil];
    DetailViewController *detailView = (DetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

    // Get the inner array for this section
    NSArray *innerArray = [self.outerArray objectAtIndex:indexPath.section];

    // Get the name from the inner array
    NSString *tmpname = [innerArray objectAtIndex:indexPath.row];
    NSInteger indx = [self.namesDictionary indexOfObjectPassingTest:^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
        return [dict[@"name"] isEqualToString:tmpname];
    }];
    NSDictionary *dict = self.namesDictionary[indx];
    detailView.passedInDictionary = dict;

    [self presentViewController:detailView animated:YES completion:nil];
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top