tableView: didSelectRowAtIndexPath не срабатывает после вызова жеста смахивания - PullRequest
4 голосов
/ 06 марта 2012

Это локализованная проблема. Я собираюсь опубликовать много кода и дать много объяснений. Надеюсь ... кто-то может помочь мне с этим.

В моем приложении у меня есть меню в стиле Facebook. Приложение iOS для Facebook, чтобы быть более конкретным. Вы можете получить доступ к этому меню двумя различными способами. Вы можете либо нажать кнопку меню, либо проведите пальцем, чтобы открыть меню. Когда кто-либо открывает и закрывает меню с помощью кнопки, метод tableView:didSelectRowAtIndexPath прекрасно срабатывает при прикосновении к ячейке. Когда кто-то открывает и закрывает меню, используя метод салфетки ... это не так. Вы должны коснуться ячейки таблицы дважды, чтобы метод сработал. Код для этих методов одинаков в нескольких классах, однако это единственный вопрос, с которым у меня возникла проблема. Взглянуть; посмотрим, брошу ли я куда-нибудь мяч:

#import "BrowseViewController.h"



@implementation BrowseViewController

@synthesize browseView, table, countriesArray, btnSideHome, btnSideBrowse, btnSideFave, btnSideNew, btnSideCall, btnSideBeset, btnSideEmail, btnSideCancelled, menuOpen, navBarTitle, mainSearchBar, tap;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = @"Countries";
self.navBarTitle.titleView = label;
[label sizeToFit];

CheckNetworkStatus *networkCheck = [[CheckNetworkStatus alloc] init];
BOOL internetActive = [networkCheck checkNetwork];

if (internetActive) {

    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
    tap.delegate = self;
    tap.cancelsTouchesInView = NO;

    UISwipeGestureRecognizer *oneFingerSwipeLeft = 
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
    [oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [[self view] addGestureRecognizer:oneFingerSwipeLeft];

    UISwipeGestureRecognizer *oneFingerSwipeRight = 
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
    [oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [[self view] addGestureRecognizer:oneFingerSwipeRight];

    menuOpen = NO;
    table.userInteractionEnabled = YES;
    NSArray *countries = [[NSArray alloc] initWithObjects:@"United States", @"Canada", @"Mexico", nil];
    self.countriesArray = countries;
} else {
    //No interwebz, notify user and send them to the home page
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Failed to connect to the server. Please verify that you have an active internet connection and try again. If the problem persists, please call us at **********" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [message show];
    PassportAmericaViewController *homeView = [[PassportAmericaViewController alloc]
                                               initWithNibName:@"PassportAmericaViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:homeView animated:YES];
}

[super viewDidLoad];
}


-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection: (NSInteger)section
{
  return [countriesArray count];
  NSLog(@"Number of objecits in countriesArray: %i", [countriesArray count]);
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"CellIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      cell.textLabel.textColor = [UIColor whiteColor];
      cell.textLabel.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:20.0];
  }


  NSUInteger row = [indexPath row];

  cell.textLabel.text = [countriesArray objectAtIndex:row];

  return cell;
}

- (void)tableView:(UITableView *)table
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *countrySelected = [countriesArray objectAtIndex:indexPath.row];
Campground *_Campground = [[Campground alloc] init];
_Campground.country = countrySelected;

StateViewController *stateView = [[StateViewController alloc]
                                    initWithNibName:@"StateView" bundle:[NSBundle mainBundle]];
stateView._Campground = _Campground;

[self.navigationController pushViewController:stateView animated:YES];

}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(void) dismissKeyboard {

[mainSearchBar resignFirstResponder];

}

-(IBAction)goBack:(id)sender{

[self.navigationController popViewControllerAnimated:YES];

}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:@"slideMenu"]){
    UIView *sq = (__bridge UIView *) context;
    [sq removeFromSuperview];

}
}

- (IBAction)menuTapped {
NSLog(@"Menu tapped");
CGRect frame = self.browseView.frame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector( animationDidStop:finished:context: )];
[UIView beginAnimations:@"slideMenu" context:(__bridge void *)(self.browseView)];

if(!menuOpen) {
    frame.origin.x = -212;
    menuOpen = YES;
    table.userInteractionEnabled = NO;
}
else
{
    frame.origin.x = 0;
    menuOpen = NO;
    table.userInteractionEnabled = YES;
}

self.browseView.frame = frame;
[UIView commitAnimations];
}

-(IBAction) sideHome:(id)sender{

CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;
menuOpen = NO;
PassportAmericaViewController *homeView = [[PassportAmericaViewController alloc]
                                           initWithNibName:@"PassportAmericaViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView animated:YES];
table.userInteractionEnabled = YES;

}
-(IBAction) sideBrowse:(id)sender{

CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;
menuOpen = NO;
BrowseViewController *browseView2 = [[BrowseViewController alloc]
                                    initWithNibName:@"BrowseView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:browseView2 animated:YES];
table.userInteractionEnabled = YES;

}
-(IBAction) sideBeset:(id)sender{

CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;
menuOpen = NO;
BesetCampgroundMapViewController *besetMapView = [[BesetCampgroundMapViewController alloc]
                                                  initWithNibName:@"BesetCampgroundMapView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:besetMapView animated:YES];
table.userInteractionEnabled = YES;

}
-(IBAction) sideFave:(id)sender{

CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;
menuOpen = NO;
FavoritesViewController *faveView = [[FavoritesViewController alloc] initWithNibName:@"FavoritesView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:faveView animated:YES];
table.userInteractionEnabled = YES;


}
-(IBAction) sideNew:(id)sender{

CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;
menuOpen = NO;
NewCampgroundsViewController *theNewCampView = [[NewCampgroundsViewController alloc]
                                                initWithNibName:@"NewCampgroundsView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:theNewCampView animated:YES];
table.userInteractionEnabled = YES;

}
-(IBAction) sideCancelled:(id)sender{

CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;
menuOpen = NO;
CancelledCampgroundsViewController *cancCampView = [[CancelledCampgroundsViewController alloc]
                                                    initWithNibName:@"CancelledCampgroundsView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:cancCampView animated:YES];
table.userInteractionEnabled = YES;

}
-(IBAction) sideCall:(id)sender{

NSLog(@"Calling Passport America...");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:**********"]];
table.userInteractionEnabled = YES;

}
-(IBAction) sideEmail:(id)sender{

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"mailto:***************"]];
table.userInteractionEnabled = YES;

}

-(void) searchBarSearchButtonClicked: (UISearchBar *)searchBar {
SearchViewController *search = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:[NSBundle mainBundle]];
NSString *searchText = [[NSString alloc] initWithString:mainSearchBar.text];
search.searchText = searchText;
[self dismissKeyboard];
[self.navigationController pushViewController:search animated:YES];
table.userInteractionEnabled = YES;
menuOpen = NO;
CGRect frame = self.browseView.frame;
frame.origin.x = 0;
self.browseView.frame = frame;

}

-(void) swipeLeft:(UISwipeGestureRecognizer *)recognizer {
if (!menuOpen) {
    CGRect frame = self.browseView.frame;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector( animationDidStop:finished:context: )];
    [UIView beginAnimations:@"slideMenu" context:(__bridge void *)(self.browseView)];
    frame.origin.x = -212;
    menuOpen = YES;
    self.browseView.frame = frame;
    table.userInteractionEnabled = NO;
    [UIView commitAnimations];

} else {
    //menu already open, do nothing
}
}

-(void) swipeRight:(UISwipeGestureRecognizer *)recognizer {
if (!menuOpen) {
    //menu closed, do nothing
} else {
    CGRect frame = self.browseView.frame;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector( animationDidStop:finished:context: )];
    [UIView beginAnimations:@"slideMenu" context:(__bridge void *)(self.browseView)];
    frame.origin.x = 0;
    menuOpen = NO;
    self.browseView.frame = frame;
    table.userInteractionEnabled = YES;
    [UIView commitAnimations];

}
}

- (void) viewWillDisappear:(BOOL)animated {
[self.table deselectRowAtIndexPath:[self.table indexPathForSelectedRow] animated:animated];
[super viewWillDisappear:animated];
}


- (void)didReceiveMemoryWarning {
NSLog(@"Memory Warning!");
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
self.table = nil;
self.countriesArray = nil;
self.browseView = nil;

[super viewDidUnload];

}

@end

1 Ответ

0 голосов
/ 07 марта 2012

Определите, в какой ячейке происходит свайп, вычислите путь индекса и вызовите didSelectRowAtIndexPath из вашего кода gestRecognizer.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...