Swiping in Tableviewcell - iphone

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];
}

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 ...
}

Related

How to call gesture swipe method from another method in Objective-c

I have this gesture swipe method that I want to call from another method using
[self performSelector:#selector(handleSwipeGesture:)
withObject:nil afterDelay:10];
I can't figure out the syntax to put in the #selector()
any help is appreciated. here's my code:
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
if(sender.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"swipe left");
TutorialMenuViewController *tutorialMenuViewController = [[TutorialMenuViewController alloc]
initWithNibName:#"TutorialMenuViewController" bundle:nil];
[self.navigationController pushViewController:tutorialMenuViewController animated:YES];
[tutorialMenuViewController release];
}
}
If you want to present TutorialMenuViewController either on a gesture or time delay you would be better off just abstracting its presentation out into a different method
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
if(sender.direction == UISwipeGestureRecognizerDirectionLeft) {
[self presentTutorial];
}
}
- (void)presentTutorial;
{
TutorialMenuViewController *tutorialMenuViewController = [[TutorialMenuViewController alloc]
initWithNibName:#"TutorialMenuViewController" bundle:nil];
[self.navigationController pushViewController:tutorialMenuViewController animated:YES];
[tutorialMenuViewController release];
}
Now you can simply call
[self performSelector:#selector(presentTutorial) withObject:nil afterDelay:10];
- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
if(sender.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(#"swipe left");
//TutorialMenuViewController *tutorialMenuViewController = [[TutorialMenuViewController alloc]
// initWithNibName:#"TutorialMenuViewController" bundle:nil];
//[self.navigationController pushViewController:tutorialMenuViewController animated:YES];
//[tutorialMenuViewController release];
}
}
Called it like this:
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:nil action:nil];
[leftSwipe setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self performSelector:#selector(handleSwipeGesture:) withObject:leftSwipe afterDelay:1];
[leftSwipe release];

how can we limit gesture in iphone?

I have Three type of gestures in my view(tableview is my view)
PinchGesture:redirecting to another page
LeftSwipe:for next chapter
RightSwipe:for previos chapter
Scrolling:tableview scrolling
My requirement is when any one of the gesture acts the other gestures including scrolling of tableview have to be disabled,Is this possible?
my code for gesture is
-(void) handleSwipeGesture:(UISwipeGestureRecognizer*)recognizer {
if(![delegate.selectedChapter isEqualToString:[NSString stringWithFormat:#"%d",[DbHandler mNumberOfChaptersInBook:delegate.selectedBook]]]) {
// if the currentChapter is the last then do nothing
delegate.selectedChapter = [NSString stringWithFormat:#"%d",[delegate.selectedChapter intValue] + 1];
[delegate reloadVerses];
[self resetReadViewToVerse:1];
}
return;
}
-(void) handleSwipeGestureleft:(UISwipeGestureRecognizer*)recognizer {
if(![delegate.selectedChapter isEqualToString:#"1"])
{
delegate.selectedChapter = [NSString stringWithFormat:#"%d",[delegate.selectedChapter intValue] - 1];
[delegate reloadVerses];
[self resetReadViewToVerse:1];
}
return;
}
-(void) longPressDetected:(UISwipeGestureRecognizer*)recognizer {
SearchViewController *aSecondViewController = [[SearchViewController alloc] initWithNibName:#"SearchViewController" bundle:nil];
aSecondViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:aSecondViewController animated:YES];
/*[self.navigationController pushViewController:aSecondViewController animated:YES];*/
[aSecondViewController release];
[UIView commitAnimations];
}
When your gesture recognizer method is called you can try.
if (recognizer.state==UIGestureRecognizerStateEnded) {
//Do your thing.
}
HTH.

PageController and TableView

I have a page controller and that method pageAction defined like this :
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
[self getVehicules];
[self.tableViewVehiculesPossedes release];
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
[self loadScrollViewWithPage:page];
CGRect frame = pageControlVehiculePossedee.frame;
frame.origin.x = (vosvehiculeScrollView.frame.size.width * page);
[vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
Every time I change the page I need to display a tableview with some labels. Here is the code :
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[self loadScrollViewWithPage:0];
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
votreVehiculeLabel.text=#"Votre véhicule";
vehiculesPossedesArray = [[NSMutableArray alloc] initWithObjects:#"Annee modele", #"Transmission",#"Carburant", nil];
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
tableViewVehiculesPossedes=[[UITableView alloc] initWithFrame:CGRectMake(3, 80, 315, 171) style:UITableViewStyleGrouped];
tableViewVehiculesPossedes.tag=page;
tableViewVehiculesPossedes.bounces=NO;
tableViewVehiculesPossedes.backgroundColor=[UIColor clearColor];
[tableViewVehiculesPossedes setDelegate:self];
[tableViewVehiculesPossedes setDataSource:self];
[self.vosvehiculeScrollView addSubview:tableViewVehiculesPossedes];
nameVehiculeLabel.text=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele"];
self.transmissionString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"transmision"];
self.carburantString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"carburant"];
self.anneeModelString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele_annee"];
self.anneeString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"annee"];
if(page==0){
NSLog(#"0");
self.transmissionString=#"ttt";
}
else NSLog(#"1");
}
The problem is that even I put [self.tableViewVehiculesPossedes release]; on the change page method, the tableView appear overlay and the text from labels are overlay. What can I do with the tableview to make it dissapear when a new page will be display?
Please help me..I spent a lot of time with this :|
If I get it correctly I think you should keep an ivar to the table you create with the alloc in loadScrollViewWithPage, and when switching page you should removing it from the view by calling :
[mytableview removeFromSuperview];
And most likely your should take care of releasing it as well as I can't see it in your code.

Iphone Web view inside scroll view

I have a small application with some detail controller. Detail controller is a web view put inside scroll view:
Here is a code of detail view controller:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= detailTotalJobs)
return;
// replace the placeholder if necessary
SearchJobDetailWebViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
NSLog(#"Vytvaram web controller: %d", page + 1);
if (detailJobIndex == page) {
controller = [[SearchJobDetailWebViewController alloc] initWithNibName:#"SearchJobDetailWebViewController" bundle:nil page:page url:detailExportUrl];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
} else if (detailJobIndex + 1 == page) {
controller = [[SearchJobDetailWebViewController alloc] initWithNibName:#"SearchJobDetailWebViewController" bundle:nil page:page url:detailNextExportUrl];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
} else {
[self performSelector:#selector(downloadData:) withObject:[NSNumber numberWithInt:page] afterDelay:0.1];
//[self downloadData:[NSNumber numberWithInt:page]];
controller = [[SearchJobDetailWebViewController alloc] initWithNibName:#"SearchJobDetailWebViewController" bundle:nil page:page url:nextArray.exportUrl];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
//NSDictionary *numberItem = [self.contentList objectAtIndex:page];
//controller.numberImage.image = [UIImage imageNamed:[numberItem valueForKey:ImageKey]];
//controller.myLabel.text = #"Test";
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed)
{
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
NSString *detailTitleText = [NSString stringWithFormat:#"%d / %d", page + 1, detailTotalJobs];
[self setTitle:detailTitleText];
//[detailTitleText release];
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
Code inspired by Apple page control.
Problem is here:
if (detailJobIndex == page) {
controller = [[SearchJobDetailWebViewController alloc] initWithNibName:#"SearchJobDetailWebViewController" bundle:nil page:page url:detailExportUrl];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
} else if (detailJobIndex + 1 == page) {
controller = [[SearchJobDetailWebViewController alloc] initWithNibName:#"SearchJobDetailWebViewController" bundle:nil page:page url:detailNextExportUrl];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
} else {
[self performSelector:#selector(downloadData:) withObject:[NSNumber numberWithInt:page] afterDelay:0.1];
//[self downloadData:[NSNumber numberWithInt:page]];
controller = [[SearchJobDetailWebViewController alloc] initWithNibName:#"SearchJobDetailWebViewController" bundle:nil page:page url:nextArray.exportUrl];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
First part and second part of if work correctly, because I have data for first web view and for second web view.
But last part of if have a problem. when I scroll from second web view to third web view, scroll view show blank page. Next pages work correctly.
Where is problem, please ?
Just put a breakpoint in debug mode on the braces.
Is controller nil ?
Check your custom init method.
Does it return nil if there is a problem ?
[self = super initWithNibName..................];
if (!self) return nil;
// some inits
return self;
If this is done and controller is nil, check your URL, and try it into a web browser. Does it showsup ?
How do you fill your webView ? Is there an error returned when filling it ? No encoding problem returned ?
Well, some code would help, but these indications are the most common problems you can have.

activity indicator on button click

I am making an iphone app, in one of the view I am trying to put the activity indicator on button click. Whenever user clicks on button, indicator must starts and at the end of this button activity indicator must stop. I have tried to implement it. But it is not working properly.
If someone has the solution for it. Please let me know.
-(void)temp
{
spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
[timetable addSubview:spinner];
[spinner startAnimating];
[spinner release];
}
-(IBAction)datetimeis:(id)sender
{
[self temp];
for (int d=0 ; d<=[totaltimearray count]-1; d++)
{
for (int e=0 ; e<=[appendarray count]-1; e++)
{
appenddate = [appendarray objectAtIndex:e];
datestring = [NSString stringWithFormat:#"%#",appenddate];
appendtime = [totaltimearray objectAtIndex:d];
timestring = [NSString stringWithFormat:#"%#",appendtime];
rslttwostr = [datestring stringByAppendingFormat:#"%#",timestring];
NSLog(#"timestring is:%#",rslttwostr);
[dateandtime addObject:rslttwostr];
NSLog(#"dateandtimeis:%#",dateandtime);
}
}
totaltimewithdate = dateandtime;
[self savedetails];
[self docmedname];
[self retrivenotificationarray];
//[barButton release];
[self.navigationController popToRootViewControllerAnimated:YES];
}
Thanks alot.
Place this line at start of button action...
[NSThread detachNewThreadSelector:#selector(StartActivityIndicator) toTarget:self withObject:nil];
add this method...
-(void)StartActivityIndicator
{
[activityIndicator startAnimating];
}