iOS-indexPathForRowAtPoint don';t返回具有不同单元格高度的正确indexPath
我有一个包含许多单元格的UITableView。用户可以通过按此单元格中的展开按钮展开单元格以查看此单元格中的更多内容(一次只能展开一个单元格):iOS-indexPathForRowAtPoint don';t返回具有不同单元格高度的正确indexPath,ios,uitableview,Ios,Uitableview,我有一个包含许多单元格的UITableView。用户可以通过按此单元格中的展开按钮展开单元格以查看此单元格中的更多内容(一次只能展开一个单元格): 在故事板中,我将UILongPress手势拖到单元格按钮中,并将其命名为longPress(单元格是自定义的,其中有两个按钮,一个需要识别longPress手势,另一个需要扩展单元格高度): 在viewDidLoad中: - (void)viewDidLoad { [longPress addTarget:self action:
在故事板中,我将UILongPress手势拖到单元格按钮中,并将其命名为longPress(单元格是自定义的,其中有两个按钮,一个需要识别longPress手势,另一个需要扩展单元格高度): 在viewDidLoad中:
- (void)viewDidLoad
{
[longPress addTarget:self action:@selector(handleLongPress:)];
}
它工作得很好,但是,当我使用以下代码识别单元格indexath时,扩展一个单元格是错误的:
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
// Get index path
slidePickerPoint = [sender locationInView:self.tableView];
NSIndexPath *indexPath= [self.tableView indexPathForRowAtPoint:slidePickerPoint];
// It's wrong when 1 cell is expand and the cell's button I hold is below the expand button
}
有人能告诉我当单元格高度不同时如何获得正确的indexPath吗?提前感谢
首先将长按手势识别器添加到表格视图:
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
{
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
NSLog(@"long press on table view at row %d", indexPath.row);
}
}
您必须小心操作,以免干扰用户正常敲击手机,同时还要注意,在用户抬起手指之前,handleLongPress可能会触发多次
谢谢 一种方法是将UILongPressGestureRecognitor添加到每个UITableViewCell(所有UITableViewCell都使用相同的选择器),然后在调用选择器时,您可以通过sender.view获取该单元格。也许不是最有效的内存,但如果单手势识别器在某些情况下无法返回正确的行,这种方法应该可以工作 大概是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
[longPress setMinimumPressDuration:2.0];
[cell addGestureRecognizer:longPress];
[longPress release];
return cell;
}
然后
请添加一些代码,说明您如何添加
UILongPressGestureRecognitor
以及如何处理单元格的扩展,否则您将得不到很好的答案。谢谢。我已经更新了我的问题。本教程不涉及UIgestureRecognitor,他想用它来检测长按。似乎无法解决他的问题。这只是我的想象,还是你的答案与他当前的代码完全相同(在扩展单元上不起作用)?@Dinesh不要担心投票……担心你的答案的质量谢谢你的答案,这对我帮助很大
-(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
NSLog(@"long press on table view at row %d", indexPath.row);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
[longPress setMinimumPressDuration:2.0];
[cell addGestureRecognizer:longPress];
[longPress release];
return cell;
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
UITableViewCell *selectedCell = sender.view;
}