Access a UITableView cell and swipe between them from DetailView using Xcode 4

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:

 #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, 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.

0
source share
1 answer

One way to do this is to pass a "sorted" array of data sources to the DetailViewController and an index path 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; } } 

You can then reload the interface elements of the DetailViewController element. Here is the announcement 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 

Remember to synthesize 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 

Try it, this may work for you. Otherwise, I think you can take a look at Scrollview and "paging". IPhone / iPad users use this user interface design, and you can change it to suit what you do.

+3
source

Source: https://habr.com/ru/post/1397441/


All Articles