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可能会触发多次(这是由于手势识别器状态更改)。
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);
}
}
}
}
- 3 回答
- 0 关注
- 922 浏览
添加回答
举报