Hi all! I'm struggling trying to populate a TableView and a DetailView from a plist.
My app's structure is:
Curiosita.h
RecipeTableViewController.h
RecipeTableViewController.m
RecipeDetailViewController.h
RecipeDetailViewController.m
RecipeTableCell.h
My plist is:
http://awesomescreenshot.com/0422ozk7f2
http://awesomescreenshot.com/08f2ozkfbf
After all, I want to change data structure as it:
http://awesomescreenshot.com/05c2ozl51d
Someone can help me? Thanks a lot in advance!
My app's structure is:
Curiosita.h
Code:
@interface Curiosita : NSObject
@property (nonatomic, strong) NSString *titolo; // titolo della curiosità
@property (nonatomic, strong) NSString *testo; // testo della curiosità
@end
RecipeTableViewController.h
Code:
@interface RecipeTableViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (strong,nonatomic) NSMutableArray *filteredTipsArray;
@property IBOutlet UISearchBar *curiositaSearchBar;
RecipeTableViewController.m
Code:
@interface RecipeTableViewController () <UITableViewDataSource, UITableViewDelegate>
@end
@implementation RecipeTableViewController
{
NSArray *titoloArray;
NSArray *testoArray;
NSArray *curiosita;
NSArray *searchResults;
}
@synthesize curiositaSearchBar;
@synthesize filteredTipsArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Find out the path of recipes.plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"curiositaPlist" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
titoloArray = [dict objectForKey:@"Titolo"];
testoArray = [dict objectForKey:@"Testo"];
//Initialize the recipes array
Curiosita *curiosita1 = [Curiosita new];
curiosita1.titolo = [titoloArray objectAtIndex:0];
curiosita1.testo = [testoArray objectAtIndex:0];
Curiosita *curiosita2 = [Curiosita new];
curiosita2.titolo = [titoloArray objectAtIndex:1];
curiosita2.testo = [testoArray objectAtIndex:1];
curiosita = [NSArray arrayWithObjects: curiosita1, curiosita2, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [curiosita count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 71;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomTableCell";
RecipeTableCell *cell = (RecipeTableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //aggiunto forIndexPath:indexPath per popolare la table view dal plist
// Configure the cell...
if (cell == nil)
{
cell = [[RecipeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Curiosita *recipe = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
recipe = [searchResults objectAtIndex:indexPath.row];
} else {
recipe = [curiosita objectAtIndex:indexPath.row];
}
cell.titoloLabel.text = recipe.titolo;
cell.testoLabel.text = recipe.testo;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = nil;
Curiosita *recipe = nil;
if (self.searchDisplayController.active) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
recipe = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
recipe = [curiosita objectAtIndex:indexPath.row];
}
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.curiosita = recipe;
}
}
-(IBAction)goToSearch:(id)sender {
// If you're worried that your users might not catch on to the fact that a search bar is available if they scroll to reveal it, a search icon will help them
// If you don't hide your search bar in your app, don’t include this, as it would be redundant
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[curiositaSearchBar becomeFirstResponder];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"testo contains[c] %@", searchText];
searchResults = [curiosita filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
@end
RecipeDetailViewController.h
Code:
@property (weak, nonatomic) IBOutlet UITextView *titoloDetail;
@property (weak, nonatomic) IBOutlet UITextView *testoDetail;
@property (nonatomic, strong) Curiosita *curiosita;
@end
RecipeDetailViewController.m
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.titoloDetail.text = self.curiosita.titolo;
self.testoDetail.text = self.curiosita.testo;
}
RecipeTableCell.h
Code:
@interface RecipeTableCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *titoloLabel;
@property (nonatomic, weak) IBOutlet UILabel *testoLabel;
@end
My plist is:
http://awesomescreenshot.com/0422ozk7f2
http://awesomescreenshot.com/08f2ozkfbf
After all, I want to change data structure as it:
http://awesomescreenshot.com/05c2ozl51d
Someone can help me? Thanks a lot in advance!