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