I have a plist that contains key and value... let say:
key: alpha_male_body_language
value: Alpha Male Body Language
key: building_attraction
value: Building Attraction
key: fifteen_lessons
value: Fifteen Lessons
key: how_can_it_have_gone_wrong
value: How can It Have Gone Wrong
Here is my implementation of tableview:
#import "BookTitleViewController.h"
#import "BookLessonViewController.h"
@interface BookTitleViewController ()
@end
@implementation BookTitleViewController{
NSDictionary *bookTitles;
}
@synthesize tableView;
@synthesize bookTitles;
- (void)viewDidLoad
{
[super viewDidLoad];
self.bookTitles = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"book" ofType:@"plist"]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.bookTitles = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.bookTitles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *value = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
//cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
cell.textLabel.text = value;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowBookLesson"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BookLessonViewController *destViewController = segue.destinationViewController;
destViewController.bookTitleKey = [[self.bookTitles allKeys] objectAtIndex:indexPath.row];
destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
//destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
}
}
@end
I have two problems using the code above:
The list is not shown in order after reading it from plist. For example, title "How can It have gone wrong" is shown on the first list, knowing the fact that it is listed on the 4th.
I get exception saying:
-[UINavigationController setBookTitleValue:]: unrecognized selector sent to instance 0x894c230
this is referring to the line:
destViewController.bookTitleKey = [[self.bookTitles allKeys] objectAtIndex:indexPath.row];
destViewController.bookTitleValue = [[self.bookTitles allValues] objectAtIndex:indexPath.row];
(Inside prepareForSegue function).
Please helps me to solve this problem. Thank you and appreciate it!
More detail on possible solution to problem 2:
UIStoryboardSegue+MMNavigationController.h
to your project, found in this Github repoprepareForSegue:sender:
like so:BookLessonViewController *destViewController = segue.topLevelDestinationViewController;
Explanation: if a storyboard segue points points to a destination view controller that is wrapped in a UINavigationController
, then the segue's destinationViewController
will value will be the navigation controller object. The code referenced here will handle this by checking whether destinationViewController
is an instance of the UINavigationController
class, and if so, will return its topViewController
instead. This is all implemented as a on the UIStoryboardSegue
class; categories are a way to add methods or properties to an existing class to extend its functionality without needing to use inheritance, etc.