UILongPressGestureRecognizer代码摘抄

本文介绍如何在iOS应用中使用UITableView,并为单元格添加长按手势识别器以显示上下文菜单,允许用户通过电子邮件分享选定内容。文章详细解释了如何配置手势识别器、处理长按事件以及创建和展示菜单。

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    

    static NSString *QuoteCellIdentifier = @"QuoteCellIdentifier";

    

    QuoteCell *cell = (QuoteCell*)[tableView dequeueReusableCellWithIdentifier:QuoteCellIdentifier];

    

    if (!cell) {

        

        UINib *quoteCellNib = [UINib nibWithNibName:@"QuoteCell" bundle:nil];

        [quoteCellNib instantiateWithOwner:self options:nil];

        cell = self.quoteCell;

        self.quoteCell = nil;

        

        if ([MFMailComposeViewController canSendMail]) {

            UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

            [cell addGestureRecognizer:longPressRecognizer];        

        }

else {

NSLog(@"Mail not available");

}

    }

    

    Play *play = (Play *)[[self.sectionInfoArray objectAtIndex:indexPath.section] play];

    cell.quotation = [play.quotations objectAtIndex:indexPath.row];

    

    return cell;

}



#pragma mark Handling long presses


-(void)handleLongPress:(UILongPressGestureRecognizer*)longPressRecognizer {

    

    /*

     For the long press, the only state of interest is Began.

     When the long press is detected, find the index path of the row (if there is one) at press location.

     If there is a row at the location, create a suitable menu controller and display it.

     */

    if (longPressRecognizer.state == UIGestureRecognizerStateBegan) {

        

        NSIndexPath *pressedIndexPath = [self.tableView indexPathForRowAtPoint:[longPressRecognizer locationInView:self.tableView]];

        

        if (pressedIndexPath && (pressedIndexPath.row != NSNotFound) && (pressedIndexPath.section != NSNotFound)) {

            [self becomeFirstResponder];

            UIMenuController *menuController = [UIMenuController sharedMenuController];

            EmailMenuItem *menuItem = [[EmailMenuItem alloc] initWithTitle:@"Email" action:@selector(emailMenuButtonPressed:)];

            menuItem.indexPath = pressedIndexPath;

            menuController.menuItems = [NSArray arrayWithObject:menuItem];

            [menuController setTargetRect:[self.tableView rectForRowAtIndexPath:pressedIndexPath] inView:self.tableView];

            [menuController setMenuVisible:YES animated:YES];

        }

    }

}



-(IBAction)deleteButtonHandle:(id)sender {

    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];

    NSIndexPath *index = [self.tableView indexPathForCell:cell];

    int row = index.row;

    int section = index.section;


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值