我有一个显示自定义视图单元格的tableview。在viewWillAppear中,我设置了一个位于UITableView上的长按手势识别器。我的长按是开始并显示有关长按的细胞的信息。然而,当我放开按下时,didSelectRowAtIndexPath方法正在触发。有没有办法在长按激发后取消触摸,以便选择行不会被触发?
我见过didSelectRowAtIndexPath called after long press,这个问题似乎没有足够的答案来解决问题。
@implementation ViewController
UILongPressGestureRecognizer *lpgr;
.
.
.
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// setup long press
lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 0.5; //seconds
lpgr.delegate = self;
lpgr.cancelsTouchesInView = true;
[self.myTableview addGestureRecognizer:lpgr];
[self.myTableview.panGestureRecognizer requireGestureRecognizerToFail:lpgr]; ...
.
.
.
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
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 {
UITableViewCell *cell = [self.myTableview cellForRowAtIndexPath:indexPath];
CensusData *currentUser;
if(self.isFiltered){
currentUser = (CensusData*)[self.filteredTableData objectAtIndex:indexPath.row];
}else{
currentUser = (CensusData*)[self.dataArray objectAtIndex:indexPath.row];
}
NSLog(@"CURRENT ROW WITH LONG PRESS: %@", currentUser.friendlyName);
}
}
}
.
.
.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
答案 0 :(得分:2)
当手势处于活动状态(已开始但尚未结束)时,禁用表格视图上的选择...
- (void)handleLongPress:(UILongPressGestureRecognizer *)gr {
if (gr.state == UIGestureRecognizerStateBegan) {
self.myTableview.allowsSelection = NO;
} else if (gr.state == UIGestureRecognizerStateEnded) {
self.myTableview.allowsSelection = YES;
}
}
无需设置委托,设置cancelsTouches或实现shouldRecognize ...(除非您需要其他内容)。
编辑此vc是一项最低限度的完整测试。它需要一个故事板,其中一个表视图连接到插座,而vc作为数据源和委托......
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property(weak,nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.tableView addGestureRecognizer:gr];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"selected %@", indexPath);
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gr {
if (gr.state == UIGestureRecognizerStateBegan) {
NSLog(@"long press began");
self.tableView.allowsSelection = NO;
} else if (gr.state == UIGestureRecognizerStateEnded) {
NSLog(@"long press ended");
self.tableView.allowsSelection = YES;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 1 :(得分:0)
您可以禁用tableview,然后只有longGesture正常工作
<form id="form1" runat="server">
<noscript>
<div>
You must enable javascript to continue.
</div>
</noscript>
<div>
<asp:Label ID="lblLabel" runat="server"></asp:Label>
<button id="btn_do" type="button">toggle</button>
</div>
<div></div>
<div id="divchatID" class="div_chat" style="height: 500px; width: 500px; background-color: #82adf2; display: none; padding:5px;">
<label id="lblvalue1">Value 1</label>
<input id="value1" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
<br />
<label id="lblvalue2">Value 2</label>
<input id="value2" type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
<button id="btnplus" type="button" > ADD</button>
<br />
<asp:Label ID="lbltext" runat="server">tesst</asp:Label>
</div>
</form>