Access UITableView cell and swipe between them from DetailView using XCode 4

I’m making my first app and I’m using XCode 4 with a Storyboard. Thanks to this place, lots of tutorials, Apple’s archives databases and a bit of me, I’m slowly getting the basics together. It’s an app populated from a plist. The plist is an array with dictionaries, the dictionarires containing stings with info about different red wines in Norway. First, the plist populates a TableView. I’m using NSSortDescritor to sort the TableView and added a button to the navigation bar for resorting if I want it displayed by another value. It looks like this:

RootTableViewController.h:

  • Trying to implement network error alerts into my app?
  • NSDate, comparing two dates
  • iOS 4.2 - Printing DOC, PPT, XLS, etc. with Apple AirPrint?
  • Cannot convert value of type 'NSMutableDictionary' to type '' in coercion for google ios Analytics
  • How to move a view along a curved path in iOS
  • Since XCode 6.3 -> Nib could not be loaded
  • #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
    

    Then the DetailViewController recieves the selected object to populate the Labels and ImageViews with the data from it.

    DetailViewController.h:

    #import <UIKit/UIKit.h>
    
    @interface DetailViewController : UIViewController 
    
    @property (nonatomic, strong) IBOutlet UILabel *districtLabel;
    @property (nonatomic, strong) IBOutlet UILabel *countryLabel;
    @property (nonatomic, strong) IBOutlet UIImageView *bottleImageView;
    
    @property (nonatomic, strong) NSString *selectedObject;
    
    @end
    

    DetailViewController.m:

    #import "WinesDetailViewController.h"
    
    @interface WinesDetailViewController ()
    
    @end
    
    @implementation WinesDetailViewController
    
    @synthesize districtLabel,countryLabel,bottleImageView;
    
    @synthesize selectedObject;
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
        // Custom initialization
        }
        return self;
    }
    
    
    - (void)viewDidAppear:(BOOL)animated
    {
        self.title = [selectedObject valueForKey:@"Name"];
        [super viewDidAppear:animated];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        districtLabel.text = [selectedObject valueForKey:@"District"];
        countryLabel.text = [selectedObject valueForKey:@"Country"];
        bottleImageView.image = [UIImage imageNamed:[selectedObject valueForKey:@"Image"]];
    
    self.navigationController.navigationBar.translucent = YES;
    self.wantsFullScreenLayout = YES;
    
    //Then I’ve added recognizers for left and right swiping:
    
    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedLeft:)];
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftGesture];
    
    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedRight:)];
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightGesture];
    
    
    }
    
    //And the voids to handle the swipes:
    
    - (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
    {
    //Access previous cell in TableView
    }
    
    - (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
    {
    //Access next cell in TableView
    }
    
    
    //Some more besic code for the view..
    
    @end
    

    As you can see, I’ve added UISwipeGestureRecognizers in the DetailViewController, because I want to reload it with data from the previous cell to when swiped to the right, and next cell when swiped to the left. Now I have no idea how to handle the voids for swipe detected, how can I reach selectedRowIndex from DetailView and swipe through cells? I’m new to programming, have been trying to figure this out for a long time now, so code examples would be great so the answer don’t lead to 100 new questions if you know what I mean. Thank you so much if you can help me.

    Solutions Collect From Internet About “Access UITableView cell and swipe between them from DetailView using XCode 4”

    One way to go about this is to pass the “sorted” datasource array to the DetailViewController and the indexpath through the “prepareForSegue” method.

    RootTableViewController.h:

    - (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];
    
            //added code
            detailViewController.detailsDataSource = [[NSArray alloc]initWithArray:sortedObjects];
            detailViewController.detailIndex = selectedRowIndex.row;
        }
    }
    

    Then you could reload the UI elements of the DetailViewController.
    Here is the declaration of the new properties.

    DetailViewController.h:

    #import <UIKit/UIKit.h>
    
    @interface DetailViewController : UIViewController 
    
    @property (nonatomic, strong) IBOutlet UILabel *districtLabel;
    @property (nonatomic, strong) IBOutlet UILabel *countryLabel;
    @property (nonatomic, strong) IBOutlet UIImageView *bottleImageView;
    
    @property (nonatomic, strong) NSString *selectedObject;
    
    // added code
    @property (strong, nonatomic) NSArray *detailsDataSource;
    @property int detailIndex;
    @end
    

    Don’t forget to synthesize the new properties
    @synthesize detailsDataSource,detailIndex;

    //And the voids to handle the swipes:
    
    - (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
    {
    //Access previous cell in TableView
        if (detailIndex != 0) // This way it will not go negative
              detailIndex--;  
    
        districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"District"]];
        countryLabel.text =  [[detailsDataSource objectAtIndex: detailIndex]  valueForKey:@"Country"];
        bottleImageView.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Image"]];
    }
    
    - (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
    {
    //Access next cell in TableView
        if (detailIndex != [detailsDataSource count]) // make sure that it does not go over the number of objects in the array.
        detailIndex++;  // you'll need to check bounds 
    
        districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"District"]];
        countryLabel.text =  [[detailsDataSource objectAtIndex: detailIndex]  valueForKey:@"Country"];
        bottleImageView.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Image"]];
    }
    
    
    //Some more besic code for the view..
    
    @end
    

    Give this a try it might work for you. Otherwise, I think you might want to take a look at Scrollview and “paging.” iPhone/iPad users are used to this UI design, and you might be able to modify it to fit what you are doing.