亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

長按UITableView

長按UITableView

iOS
拉風的咖菲貓 2020-02-04 15:42:35
我想按一下UITableViewCell以打印“快速訪問菜單”。有人已經這樣做了嗎?特別是手勢識別上UITableView?
查看完整描述

3 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

首先將長按手勢識別器添加到表格視圖中:


UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 

  initWithTarget:self action:@selector(handleLongPress:)];

lpgr.minimumPressDuration = 2.0; //seconds

lpgr.delegate = self;

[self.myTableView addGestureRecognizer:lpgr];

[lpgr release];

然后在手勢處理程序中:


-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{

    CGPoint p = [gestureRecognizer locationInView:self.myTableView];


    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];

    if (indexPath == nil) {

        NSLog(@"long press on table view but not on a row");

    } else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        NSLog(@"long press on table view at row %ld", indexPath.row);

    } else {

        NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);

    }

}

您必須注意這一點,以免干擾用戶對單元格的正常輕敲,并注意handleLongPress可能會觸發多次(這是由于手勢識別器狀態更改)。


查看完整回答
反對 回復 2020-02-04
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

我已經使用了安娜·卡列尼娜(Anna-Karenina)的答案,并且在出現嚴重錯誤的情況下效果很好。


如果您使用的是節,則長按節標題將導致您在按該節的第一行時得到錯誤的結果,我在下面添加了一個固定版本(包括根據手勢狀態過濾虛擬呼叫, Anna-Karenina的建議)。


- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {


        CGPoint p = [gestureRecognizer locationInView:self.tableView];


        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];

        if (indexPath == nil) {

            NSLog(@"long press on table view but not on a row");

        } else {

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            if (cell.isHighlighted) {

                NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);

            }

        }

    }

}


查看完整回答
反對 回復 2020-02-04
  • 3 回答
  • 0 關注
  • 956 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號