I am making my first application, and Im uses Xcode 4 with a storyboard. Thanks to this place, many textbooks, apples archive databases and a bit of me, I slowly gather the basics. His application is filled out from plist. Plist is an array with dictionaries, in dictionaries containing bites, with information about different red wines in Norway. First, plist populates the TableView. I use NSSortDescritor to sort the TableView and add a button to the navigation bar to access it if I want it to display a different value. It looks like this:
RootTableViewController.h:
#import <UIKit/UIKit.h> @interface RootTableViewController : UITableViewController <UIActionSheetDelegate> { NSMutableArray *sortedObjects; } -(IBAction)sortButtonPressed:(id)sender; @end
RootTableViewController.m:
#import "RootTableViewController.h" #import "ObjectCell.h" #import "DetailViewController.h" @interface RootTableViewController () @end @implementation RootTableViewController - (IBAction)sortButtonPressed:(id)sender; { UIActionSheet *sort = [[UIActionSheet alloc] //InitWithStyle etc for sheet } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSSortDescriptor *sortDesc; if (buttonIndex == 0) { sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES]; [sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]]; } if (buttonIndex == 1) { sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES]; [sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]]; } [self.tableView reloadData]; } - (void)viewDidLoad { [super viewDidLoad]; NSString *myfile = [[NSBundle mainBundle] pathForResource:@"Objects" ofType:@"plist"]; sortedObjects = [[NSMutableArray alloc]initWithContentsOfFile:myfile]; NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Popularity" ascending:YES]; [sortedObjects sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]]; [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Table view data source - (void)viewWillAppear:(BOOL)animated { [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [sortedObjects count]; } //(I'm using a Custom Cell for the TableView) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"objectCell"; ObjectCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[ObjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.nameLabel.text = [[sortedObjects objectAtIndex:indexPath.row] valueForKey:@"Name"]; cell.countryLabel.text = [[sortedObjects objectAtIndex:indexPath.row] valueForKey:@"Country"]; return cell; } #pragma mark - Table view delegate //Then the selected object is sent to the DetailViewController in this segue: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"DetailSegue"]) { NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow]; DetailViewController *detailViewController = [segue destinationViewController]; detailViewController.selectedObject = [sortedObjects objectAtIndex:selectedRowIndex.row]; } } @end
The DetailViewController then gets the selected object to populate the Labels and ImageViews with data from it.
DetailViewController.h:
DetailViewController.m:
As you can see, Ive added UISwipeGestureRecognizers to the DetailViewController because I want to reload it with data from the previous cell when it was shifted to the right, and the next cell when it was shifted to the left. Now I have no idea how to handle voids when a swipe is detected, how can I get to the selected RowIndex from the DetailView and pass through the cells? I am new to programming, trying to understand this for a long time, so the code examples would be great, so the answer would not lead to 100 new questions if you knew what I mean. Thank you so much if you can help me.
source share