我在 ViewDidLoad 方法中将 UILongPressGestureRecognizer 添加到 TableView 。我添加这个是为了检测代码中 TableView 的长按。但它永远行不通。在 ViewDidLoad 中,我添加了这段代码:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.resultTableView addGestureRecognizer:lpgr];
[lpgr release];
我还添加了这个方法:
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.resultTableView];
NSIndexPath *indexPath = [self.resultTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
}
else {
NSLog(@"long press on table view at row %d", indexPath.row);
}
}
请帮我解决这个问题?
最佳答案
您的代码正在运行。我认为您必须在 .h 文件中添加 UIGestureRecognizerDelegate
委托(delegate)或如何声明 resultTableView 我的意思是您以编程方式定义或使用 .xib 文件。检查一次。
我试过这样的。
resultTableView = [[UITableView alloc] init];
resultTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStylePlain];
resultTableView.rowHeight = 100.0;
resultTableView.delegate=self;
resultTableView.dataSource=self;
[self.view addSubview:resultTableView];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[resultTableView addGestureRecognizer:lpgr];
[lpgr release];
关于objective-c - UILongPressGestureRecognizer 不适用于 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877742/