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

被折叠的 条评论
为什么被折叠?



