簡體   English   中英

setContentOffset之后,點按手勢不起作用

[英]Tap gesture doesn't work after setContentOffset

我在UITableView上有一個UITapGestureRecognizer。 在我調用方法scrollToRowAtIndexPath:atScrollPosition:animated:之前,它的效果很好。 一旦被調用,它將不起作用。 一旦我調用setContentOffset:它也將不起作用。 我必須用手指稍微滾動一下tableView,然后輕按手勢即可工作。 設置contentOffset或滾動到一行后,如何使它工作?

-(void)viewDidLoad {
    // The gesture that doesn't work after 'setContentOffset:'
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(hideShowEditing:)];
    [tap setCancelsTouchesInView: YES];
    [tap setDelegate: self];

    [self setEditTap: tap];
    [[self tableView] addGestureRecognizer: tap];
}


-(BOOL)hideShowEditing:(UIGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView: [self tableView]];
    NSIndexPath *ip = [[self tableView] indexPathForRowAtPoint: location];
    TaskCell *cell = (TaskCell *)[[self tableView] cellForRowAtIndexPath: ip];

    // No cells are expanded, so expand this one
    if (editingCellRow == -1 && editingCellSection == -1 && ![gesture isKindOfClass: [UISwipeGestureRecognizer class]]) {

        editingCellRow = [ip row];                          // if it's a swipe gesture, then it's from the crossOut method
        editingCellSection = [ip section];

        [[self tableView] beginUpdates];
        [[self tableView] endUpdates];

        [cell addViewsForEditing];

        // Tap gesture only doesn't work if it has to scroll the tableView
        [[self tableView] scrollToRowAtIndexPath: ip atScrollPosition: UITableViewScrollPositionNone animated: YES];

        return YES;
    }
    // Another cell is expanded, so unexpand the other cell
    // and expand this cell
    else if ((editingCellRow != [ip row] || editingCellSection != [ip section]) && ![gesture isKindOfClass: [UISwipeGestureRecognizer class]]) {

        // Index path of the expanded cell
        NSIndexPath *expandedIp = [NSIndexPath indexPathForRow: editingCellRow inSection: editingCellSection];
        TaskCell *expandedCell = (TaskCell *)[[self tableView] cellForRowAtIndexPath: expandedIp];

        [expandedCell setAnimateExpansion: YES];
        [cell setAnimateExpansion: YES];

        // Store the index of the new expanded cell
        editingCellRow = [ip row];
        editingCellSection = [ip section];

        [[self tableView] beginUpdates];
        [[self tableView] endUpdates];

        [expandedCell removeViewsForEditing];

        [[self tableView] scrollToRowAtIndexPath: ip atScrollPosition: UITableViewScrollPositionNone animated: YES];

        [cell addViewsForEditing];

        return YES;
    }
    // Tapped the expanded cell, so unexpand it
    else if (editingCellRow == [ip row] && editingCellSection == [ip section]) {
        [cell setAnimateExpansion: YES];

        editingCellRow = -1;
        editingCellSection = -1;

        [[self tableView] beginUpdates];
        [[self tableView] endUpdates];

        [cell removeViewsForEditing];

        return YES;
    }

    return NO;
}

我不明白為什么它不適合您。 我已經測試過並且可以正常工作。 我的tableViewController是我的代表窗口的rootViewController。rootViewController這里沒什么特別的,它的想法是有一個導航欄在其中放置一個按鈕。

這是我的tableViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController <UIGestureRecognizerDelegate>

// scrolling method
- (void)move:(id)sender;

// method triggered by the tap
- (void)tapIt:(id)sender;

@end

這是我的tableViewController.m(主要部分的摘錄):

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add a bar button to call the scrolling method
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(move:)];
    self.navigationItem.rightBarButtonItem = rightBtn;

    //add tap gesture
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapIt:)];
    [tap setCancelsTouchesInView:YES];
    [tap setDelegate:self];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    [[self tableView] addGestureRecognizer:tap];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [NSString stringWithFormat:@"item %i", indexPath.row];

    return cell;
}

// the scrolling method
- (void)move:(id)sender
{
    NSLog(@"scroll");
    //scroll to 17th row for exemple
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:17 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

// method triggered by the tap gesture
- (void)tapIt:(id)sender
{
    NSLog(@"tap it");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM