Populating two grouped tableviews from the same PLIST
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 butto开发者_如何学Gons.
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".
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
精彩评论