5

我有一个 uitableview,它在点击单元格时实现弹出框(PopoverView),然后弹出框将在屏幕上的任何其他点击时关闭。问题是,如果用户要双击或重复点击单元格,它将导致显示多个弹出视图实例,然后应用程序将崩溃。我正在寻找一种方法来禁用双击单元格和/或UITableView一般来说,或者有没有办法延迟UITableViewCell任何想法的接触?

我已经尝试过了,但在我的情况下它不起作用。另一种方法是检查 PopoverView 是否已经存在,如果存在则不允许另一个实例化。我试过这个这个,但在我的情况下都不起作用。

这是我在其中调用弹出视图的代码didSelectRowAtIndexpath

- (void)tableView:(UITableView *)TableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath];
sti = [[SelectedTeamsInfo alloc] init];
MyLeagueStandings *info = [fetchedResultsController objectAtIndexPath:indexPath];
[sti getAllScheduleForTeam:info.urlforteam];
NSString *title = info.teamname;

// If title length is greater then 32 truncate it to fit.
if (title.length > 32) {
    title = [info.teamname substringToIndex:29];
    title = [title stringByAppendingString:@"..."];
}


[PopoverView showPopoverAtPoint:cell.center inView:self.view withTitle:title withContentView:sti.view delegate:self];
}

解决方案

在接口类中:

 BOOL PopoverYN;

在实现类中:

- (void)tableView:(UITableView *)TableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // If the popover is not available then display it else do nothing since one is already displayed.
        if (PopoverYN == NO) {
            PopoverYN = YES;
            UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath];
            sti = [[SelectedTeamsInfo alloc] init];
            MyLeagueStandings *info = [fetchedResultsController objectAtIndexPath:indexPath];
            [sti getAllScheduleForTeam:info.urlforteam];
            NSString *title = info.teamname;

            // If title length is greater then 32 truncate it to fit.
            if (title.length > 32) {
                title = [info.teamname substringToIndex:29];
                title = [title stringByAppendingString:@"..."];
            }
            [PopoverView showPopoverAtPoint:cell.center inView:self.view withTitle:title withContentView:sti.view delegate:self];
        }

}

#pragma mark - popover methods.
- (void)popoverViewDidDismiss:(PopoverView *)popoverView;
{
    PopoverYN = NO;
}
4

3 回答 3

4

我还有一个解决方案。希望这会对某人有所帮助。如果您想检测第二次点击并使用它,那么就是这样,这对我有用。我正在单击加载网页视图,如果连续两次点击,则错误正在获取NSURLErrorCancelled事件并导致白色闪烁屏幕webview。我可以在网络视图级别处理它,但我应该从根本上解决问题。

NSTimer *avoidDoubleTapTimer;

- (void) onTimer {  
    NSLog(@"Timer out");
    avoidDoubleTapTimer = nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(avoidDoubleTapTimer == nil) {
        avoidDoubleTapTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
    } else {
        NSLog(@"Double Tap Detected");
        return;
    }
// do you stuff on single tap
}
于 2014-04-10T21:06:29.873 回答
0

将弹出框附加到该视图上的属性。清除它何时被解除(通过委托方法)。如果didSelectRowAtIndexPath第一个没有被解雇,不要创建另一个弹出窗口。

于 2013-07-12T22:49:59.343 回答
0

这是@mask 答案的 Swift 版本,但我的withTimeInterval持续时间用 2 秒而不是 1 秒效果更好。

应该注意的是,即使这个计时器没有重复,它在我点击一个单元格后引起了问题,新的 vc 被推送,我返回到父级(有计时器的那个),然后切换选项卡。标签被冻结。在多次尝试注释和取消注释计时器后,我意识到它是计时器。

为了解决这个问题,我刚刚取消了计时器viewWillDisappear

var avoidDoubleTapTimer: Timer?

@objc func cancelDoubleTapTimer() {
    avoidDoubleTapTimer = nil
}

// use it for a cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if avoidDoubleTapTimer == nil {

        avoidDoubleTapTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { [weak self](_) in
            self?.cancelDoubleTapTimer()
        })
    } else {

        return
    }

    // run your code, do whatever ...
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    cancelDoubleTapTimer()
}

// or use it for a tapGestureRecognizer

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(labelWasTapped))
yourLabel.addGestureRecognizer(tapGesture)

@objc func labelWasTapped() {

    if avoidDoubleTapTimer == nil {

        avoidDoubleTapTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { [weak self](_) in
            self?.cancelDoubleTapTimer()
        })

    } else {

        return
    }

    // run your code, do whatever ...
}
于 2020-05-19T22:22:46.900 回答