views:

26

answers:

1

In my app, there is a portion that holds a static contacts directory. I would like the directory to appear indexed (alphabetically) and the detail view will need to be grouped also. (I am somewhat trying to replicate the look and feel of the Contacts app.) I have it working, just no index and a detail page that is just a view with a collection of buttons.

For some reason, I cannot get the a-ha moment when dealing with the table view.

Does anyone have any examples of how I can do this? Even better, what is the absolute best book to show how to work with UITableViews (especially when grouping them) using a PLIST as a source?

Apples documentation and other searches have gotten me some good information, but feels far from comprehensive enough to fully "get it".

A: 

First of if youre using pLists as the source then they wont stay in any form of order, well not the order there in the pList anyway. A way around this is to have an array within your pList which then has your elements. To us data from a plist you might want to do something like this if you had a plist populated with NSDictionarys:

NSDictionary* dict = [NSDictionary initwithContentsOfFile:@"ApList.plist"];
//Get all of the elements in the dictionary
NSArray *array = [dict allKeys};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}    

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier;

MyIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[table dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
//Create a new dict based of the indexpath of the table cell, we need the array otherwise we wouldnt be able to get the key value for the dict.
    NSDictionary* currentDict = [NSDictionary valueForKey:[array objectAtIndex:indexPath.row]];
//Then once you;ve got the dict create a string from a String held in that dict, in this case the key for the String is LABEL.
NSString *title = [currentDict valueForKey:@"LABEL"];
    cell.textLabel.text = title;

return cell;
}

Hope this helps

octermircty
Since the data is static, I can have the PLIST generated in alphabetical order. I am also not set on any PLIST format as of yet. On my detail page, like the apple contacts app, there is a section for phone numbers which vary in number, there can be 1-5 addresses (in an address section), email, etc.
TheHockeyGeek
If I were you I'd have a array of NSDictionarys inside my plist. Each Dictionary containing an array for both phone numbers and addresses. If you just put data straight into a pList even if it is sorted, it wont be read out in that order.
octermircty