Ios UILongPressGestureRecognitor在第一个单元格上不工作?
我尝试添加UILongPressGestureRecognitor,以在按下单元格时显示警报视图,但由于某些原因,它无法在表视图中的第一个单元格上工作。因此,它可以在所有其他单元格上工作,但是index path.row 0处的单元格不工作。有人知道怎么解决这个问题吗 我的代码: UIgestureRecognitzerDelegate添加到.h UILongPress手势识别器*longPress;在高层实施Ios UILongPressGestureRecognitor在第一个单元格上不工作?,ios,objective-c,Ios,Objective C,我尝试添加UILongPressGestureRecognitor,以在按下单元格时显示警报视图,但由于某些原因,它无法在表视图中的第一个单元格上工作。因此,它可以在所有其他单元格上工作,但是index path.row 0处的单元格不工作。有人知道怎么解决这个问题吗 我的代码: UIgestureRecognitzerDelegate添加到.h UILongPress手势识别器*longPress;在高层实施 - (UITableViewCell *)tableView:(UITableVie
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"activeComputers";
ActiveComputersCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ActiveComputersCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//gesture recognizer
longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectCellHold)];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];
cell.lblComputer.text = [computer objectAtIndex:indexPath.row];
cell.lblCustomer.text = [customer objectAtIndex:indexPath.row];
return cell;
}
- (void)detectCellHold{
CGPoint p = [longPress locationInView:self.tableView];
NSIndexPath *indexOfHold = [self.tableView indexPathForRowAtPoint:p];
if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (longPress.state == UIGestureRecognizerStateBegan){
NSLog(@"%ld", (long)indexOfHold.row);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Computer" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Completed", @"Invoice", nil];
[alert setTag:1];
[alert show];
}
}
更新:
当我使用:
- (void)detectCellHold{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Computer" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Completed", @"Invoice", nil];
[alert setTag:1];
[alert show];
}
但是,这会多次显示警报?添加识别器是一种不好的方法,在滚动过程中,您可以向单元格中添加新的识别器,而单元格中已经有识别器,请执行以下操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"activeComputers";
ActiveComputersCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ActiveComputersCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//gesture recognizer
longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectCellHold)];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];
}
cell.lblComputer.text = [computer objectAtIndex:indexPath.row];
cell.lblCustomer.text = [customer objectAtIndex:indexPath.row];
return cell;
}
跟进@mityaika07的回答 为什么在创建单元格时要添加手势识别器 您还可以将其添加到ActiveComputersCell类中
这样,每次长按都会自动获得特定的单元格实例。谢谢您的回复,但这根本无法工作?@JordanNewton我的建议是将手势识别器的初始化和添加移动到UITableViewCell类,在您的例子中是ActiveComputersCell,您正在尝试确定单击单元格的indexPath,如果您也在同一个类上实现了手势回调方法,则不需要这样做。请让我知道一旦你尝试这个。我已经做了,它似乎已经奏效。我该如何阻止它显示两次警报?@JordanNewton好极了,我会确保只添加一次手势,并且在ActiveComputersCell类中只实现一次手势回调方法。@JordanNewton很高兴这有帮助。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectCellHold)];
longPress.delegate = self;
[self addGestureRecognizer:longPress];