2012-03-06 162 views
4

这是一个本地化问题。我会发布很多代码,并提供很多解释。希望...有人可以帮助我。tableView:didSelectRowAtIndexPath在滑动手势后未触发

在我的应用程序中,我有一个“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 
+0

我有同样的问题,除了我以后,水龙头不会在垂直滑动后开火 – 2012-08-02 21:59:03

回答

-1

确定哪些细胞轻扫发生在,计算指数的路径,并从gestureRecognizer代码中调用didSelectRowAtIndexPath方法。

+0

滑动发生在视图上。不是细胞。滑动做了两件事。打开并关闭菜单。而已。 – tallybear 2012-03-06 21:27:17

+0

从你的描述来看,你的问题似乎是swipes没有调用didSelectRowAtIndexPath。如果这是正确的,那么您可以通过确定哪个单元格在滑动下从滑动操作代码中自行调用它。 – 2012-03-07 15:33:19

+1

这是事情......我不想滑动来调用didSelectRowAtIndexPath。滑动与tableView上发生的任何操作无关。滑动只是打开和关闭侧面菜单。问题是...一旦用户滑动关闭菜单,didSelectRowAtIndexPath方法在第一次触摸tableView上的单元格时不会触发。你必须碰它两次。我有table.userInteractionEnabled = YES;在滑动方法中关闭菜单。 – tallybear 2012-03-09 15:22:07