0

Ha ii everybody,i have a question ,how can we implement the swiping gesture functionality in tableview?My project have many chapters each chapter is loaded in tableview cell,i put go to next-chapter in a button-click and place it in the footer of the tableview and go to previous page button in the header of the tableview.everything went fine,it reloads data when user tap the buttons.But now i want this functionality in swipe the tableview cell,when the user swipe right it want to loads next chapter and vise versa.my code for next and prvous chapter is

-(IBAction)_clickbtntablenextchapter:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
-(IBAction)_clickbtntableprvchapter:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}

I just want to implement it in swipe function. thanks in advance. edit

-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer {
    // do whatever you need to do ...
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
-(void) handleSwipeGestureleft:(UISwipeGestureRecognizer*)recognizer {
    // do whatever you need to do ...
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
} 
this is gesture code and my button code is ,in button it works perfect.
-(IBAction)_clickbtntablenext:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] + 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
-(IBAction)_clickbtntableprevious:(id)sender
{
    delegate.selectedChapter = [NSString stringWithFormat:@"%d",[delegate.selectedChapter intValue] - 1];
    [delegate reloadVerses];
    [self resetReadViewToVerse:1]; 
}
ICoder
  • 1,347
  • 2
  • 13
  • 23

1 Answers1

3

The swiping behaviour is default in edit mode, you should look into Inserting and Deleting Rows in Editing Mode.

If you want to have your own behaviour you can use a UISwipeGestureRecognizer to detect the swipes.

EDIT:

Create the reconizer:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight // or whatever
[cell addGestureRecognizer:swipeGesture];
[swipeGesture release];

Implement the callback for the target:

-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer {
    // do whatever you need to do ...
}
jbat100
  • 16,757
  • 4
  • 45
  • 70
  • You can read the docs on gesture recognizers http://developer.apple.com/library/IOs/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html#//apple_ref/doc/uid/TP40009541-CH6-SW1 (add the recognizer directly to the cell, it is a UIView subclass) – jbat100 Nov 10 '11 at 09:58
  • you put it when you create your cell in the cellForRowAtIndexPath method (because you want to do it for each cell), when you get the callback from the recognizer you can know which cell was swiped (because the recognizer has a view propterty which is your cell) – jbat100 Nov 10 '11 at 10:12
  • 1
    it works perfect but one problem when we swipe it lods the chapter by double,that means 1st chapter 3 chapter 5th chapter 7th chapter and viseversa. – ICoder Nov 10 '11 at 10:30
  • sir i got the answer,i put the tablename instead of cell.thanks lottttt. – ICoder Nov 10 '11 at 10:50