Reputation: 4306
So, i have a UITableView split in 3 sections. I want to be able, once i opened up the second row in the first section (i.e.), to swipe left to go to the next cell, and to swipe right to go the previous cell.
I wrote the code for the swipe:
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 that? Is it right to put the code into the Detail View?
Upvotes: 4
Views: 1630
Reputation: 1697
I have a very simple solution for your issue.
You need to declare an NSMutableArray *arr;
in your .h file and assign your array to this array when you are moving to detail page.
And also you need to declare an NSString *currentPos;
variable.
- (void)swipeDetectedRight:(UIGestureRecognizer *)sender {
currentPos--;
NSMutableDictionary *dic=[arr objectAtIndex:currentPos];
}
- (void)swipeDetectedLeft:(UIGestureRecognizer *)sender {
currentPos++;
NSMutableDictionary *dic=[arr objectAtIndex:currentPos];
}
In this way you can get your next and prev index values of array.
Hope this help for you. Shivam
Upvotes: 3
Reputation: 119
In my example I use NSString as my data that will be displayed in detail view controller. Feel free to change that to whatever suits your needs. Okay so here we go:
First declare a protocol in DetailViewController like so:
@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 next thing is to add UISwipeGestureRecognizers in DetailViewController to check for 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 push your DetailViewController:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.label.text = self.data;
}
Don't forget to implement methods that will be called by GestureRecognizers:
- (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 thats all you need in your Detail View. Now go to the 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 is how I manage 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 have to do is to implement protocol's 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 simulator and worked fine. It is very simple as my data model is quite simple - it is just NSString. There is no checking whether there is any row in section so you have to figure that out yourself. But the whole delegation pattern should be the same.
Good luck!
Upvotes: 1
Reputation: 1406
Declare a protocol in the detail view controller and set the parent (which should be the table view controller) as the delegate. Then, in the swipe methods call the delegate and implement the necessary code for changing the selected row.
Upvotes: 0