How to go to the next cell (Detailed view) in a UITableView?

So, I have a UITableView divided into 3 sections. I want to be able, as soon as I open the second row in the first section (i.e.), Swipe left to go to the next cell, and swipe right to go to the previous cell.

I wrote the code to scroll:

SecondDetailView.m - (void)viewDidLoad { UISwipeGestureRecognizer *swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDetectedLeft:)]; swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeRecognizerLeft]; [swipeRecognizerLeft release]; UISwipeGestureRecognizer *swipeRecognizerRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDetectedRight:)]; swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRecognizerRight]; [swipeRecognizerRight release]; } - (void)swipeDetectedRight:(UIGestureRecognizer *)sender { NSLog(@"Right Swipe"); } - (void)swipeDetectedLeft:(UIGestureRecognizer *)sender { NSLog(@"Left Swipe"); } 

How can i do this? Is it correct to insert the code in the detailed view?

+4
source share
3 answers

I have a very simple solution for your problem. You need to declare NSMutableArray *arr; in your .h file and assign your array to this array when going to the details page. And also you need to declare a variable NSString *currentPos; .

 - (void)swipeDetectedRight:(UIGestureRecognizer *)sender { currentPos--; NSMutableDictionary *dic=[arr objectAtIndex:currentPos]; } - (void)swipeDetectedLeft:(UIGestureRecognizer *)sender { currentPos++; NSMutableDictionary *dic=[arr objectAtIndex:currentPos]; } 

This way you can get your next and previous array index values.

Hope this help is for you. Shivam

+3
source

In my example, I use NSString as my data, which will be displayed in the detail view controller. Feel free to modify this to suit your needs. Ok, so we go:

First declare the protocol in the DetailViewController as follows:

 @class DetailViewController; @protocol DetailViewControllerDelegate <NSObject> - (void)swipeToNextCell:(DetailViewController *)sender; - (void)swipeToPreviousCell:(DetailViewController *)sender; @end @interface DetailViewController : UIViewController @property(weak, nonatomic) id<DetailViewControllerDelegate> delegate; @property(copy, nonatomic) NSString *data; @property(weak, nonatomic) IBOutlet UILabel *label; @end 

The following is to add the UISwipeGestureRecognizers to the DetailViewController to verify gestures:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 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]; } 

Implement viewWillAppear to display your data when you click on your DetailViewController:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.label.text = self.data; } 

Remember to implement the methods that GestureRecognizers will call:

 - (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender { NSLog(@"Right Swipe"); [self.delegate swipeToNextCell:self]; self.label.text = self.data; } - (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender { NSLog(@"Left Swipe"); [self.delegate swipeToPreviousCell:self]; self.label.text = self.data; } 

And that’s all you need in your detailed presentation. Now go to TableViewController. Your TableViewController should implement the DetailViewControllerDelegate protocol:

 @interface CustomTableViewController : UITableViewController <DetailViewControllerDelegate> @property(strong, nonatomic) DetailViewController *detailViewController; @property(assign, nonatomic) NSInteger currentRow; @end 

Here is my getter for detailViewController @property:

 - (DetailViewController *)detailViewController { if (_detailViewController == nil) { _detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; _detailViewController.delegate = self; } return _detailViewController; } 

Here's how I can control row selection:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *viewController = self.detailViewController; viewController.data = [NSString stringWithFormat:@"Cell: %d", indexPath.row]; viewController.title = @"Detail"; self.currentRow = indexPath.row; [self.navigationController pushViewController:viewController animated:YES]; } 

The last thing you need to do is implement the protocol methods:

 - (void)swipeToNextCell:(DetailViewController *)sender { // Get data for next row sender.data = [NSString stringWithFormat:@"Cell: %d", ++self.currentRow]; } - (void)swipeToPreviousCell:(DetailViewController *)sender { // Get data for next row sender.data = [NSString stringWithFormat:@"Cell: %d", --self.currentRow]; } 

I tested it on a simulator and worked great. It is very simple, since my data model is quite simple - it is just NSString. There is no check to see if there is any line in the section, so you need to figure it out yourself. But the whole delegation template should be the same.

Good luck

+1
source

Declare the protocol in the detail view controller and set the parent (which should be the table view controller) as the delegate. Then, in the scroll methods, call the delegate and execute the necessary code to change the selected line.

0
source

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


All Articles