use*_*008 7 objective-c uitableview uigesturerecognizer ios
我想在桌面视图单元格上进行两次交互:正常点击和长按.我使用以下答案来帮助我开始:
问题是如果我长按一个有效的单元格,单元格将突出显示蓝色,并且长按手势不会触发(它认为它只是一个简单的点击).但是,如果我在一个无效的单元格上启动长按手势,然后将我的手指滑动到一个有效的单元格然后释放,它就可以正常工作.
Pro*_*gle 29
可能有一个更好的答案,但这是一种方法:
首先在表视图本身上创建一个长按手势识别器.
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
[self.tableView addGestureRecognizer:longPressRecognizer];
Run Code Online (Sandbox Code Playgroud)
然后,使用可以找到所选行的例程来处理它:
-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
{
if (pGesture.state == UIGestureRecognizerStateRecognized)
{
//Do something to tell the user!
}
if (pGesture.state == UIGestureRecognizerStateEnded)
{
UITableView* tableView = (UITableView*)self.view;
CGPoint touchPoint = [pGesture locationInView:self.view];
NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];
if (row != nil) {
//Handle the long press on row
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有尝试过,但我认为你可以通过让表格视图上的手势识别器等待长按失败来保持行显示选择.
Teo*_*rig -3
可能会在 IB 中或以编程方式禁用选择
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Run Code Online (Sandbox Code Playgroud)