Iphone indexPathForRowAtPoint赢得';I don’我不能给我正确的索引

Iphone indexPathForRowAtPoint赢得';I don’我不能给我正确的索引,iphone,ios4,gesture-recognition,Iphone,Ios4,Gesture Recognition,我正在尝试实现一个类似于twitter iPhone应用程序的界面。(滑动以将tableviewcell中的视图替换为自定义视图)。我正在使用苹果的uisweegesturecognifizer识别刷卡,并使用[recognizer locationInView:self.view]获取刷卡的起始位置。这给了我一个CGPoint,我将它与[tableView indexPathForRowAtPoint:location]一起使用。我的问题是,我的刷卡似乎总是在实际刷卡行上方或下方的一行被检测到

我正在尝试实现一个类似于twitter iPhone应用程序的界面。(滑动以将tableviewcell中的视图替换为自定义视图)。我正在使用苹果的
uisweegesturecognifizer
识别刷卡,并使用
[recognizer locationInView:self.view]
获取刷卡的起始位置。这给了我一个CGPoint,我将它与
[tableView indexPathForRowAtPoint:location]
一起使用。我的问题是,我的刷卡似乎总是在实际刷卡行上方或下方的一行被检测到。有没有人遇到过同样的问题

编辑:我可能还应该提到,我正在使用一个自定义的tableview单元格,它的高度比默认视图高。我不确定这是否有什么区别,我使用
heightforrowIndexAThPath:
返回高度

我的代码是-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelObject *rowData = (ModelObject *)[tempArr objectAtIndex:indexPath.row];
if (rowData.isAlternateView) {
        // Load alternateview
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [cell addGestureRecognizer:recognizer];
    [recognizer release]; 
    return cell;

}
else {
    // Load the correct uitableviewcell
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [cell addGestureRecognizer:recognizer];
    [recognizer release]; 

    return cell;
}

}


-(void) handleSwipe:(UISwipeGestureRecognizer *) recognizer
{
NSLog(@"Swipe detected!");
CGPoint location = [recognizer locationInView:self.view];   
NSIndexPath *selectedIndexPath = [tableView indexPathForRowAtPoint:location];
NSLog(@"Swipe detected at %d", selectedIndexPath.row);
ModelObject *rowData = (ModelObject *)[modelArr objectAtIndex:selectedIndexPath.row];
rowData.isAlternateView = YES;

for (int i=0; i<[tempArr count]; i++) {
    if (i!=selectedIndexPath.row) {
        ModelObject *rowToBeCleared = (ModelObject *) [modelArr objectAtIndex:i];
        if ([rowToBeCleared isAlternateView]) {
            [rowToBeCleared setIsAlternateView:NO];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:i inSection:0],nil] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:selectedIndexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
ModelObject*rowData=(ModelObject*)[tempArr objectAtIndex:indexath.row];
if(rowData.isAlternateView){
//加载交替视图
UISweepGestureRecognizer*识别器=[[UISweepGestureRecognizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[识别器设置方向:(UISwipegestureRecognitizerDirectionRight)];
[单元格地址识别器:识别器];
[识别器释放];
返回单元;
}
否则{
//加载正确的uitableviewcell
UISweepGestureRecognizer*识别器=[[UISweepGestureRecognizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[识别器设置方向:(UISwipegestureRecognitizerDirectionRight)];
[单元格地址识别器:识别器];
[识别器释放];
返回单元;
}
}
-(void)无手柄擦拭:(UISwipegestureRecognitor*)识别器
{
NSLog(@“检测到刷卡!”);
CGPoint location=[识别器位置视图:self.view];
NSIndexPath*selectedIndexPath=[tableView indexPathForRowAtPoint:location];
NSLog(@“在%d处检测到刷卡”,选择了dexpath.row);
ModelObject*rowData=(ModelObject*)[modelArr objectAtIndex:selectedIndexPath.row];
rowData.isAlternateView=是;

对于(int i=0;i首先,我建议在包含表格视图的整个视图中只添加一个手势识别器,而不是在每个单元格中添加一个手势识别器(或者如果这是一个UITableViewController,那么它们是相同的)

在viewDidLoad中:

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
然后,在HandleSweep方法中:

- (void)handleSwipe:(UISwipeGestureRecognizer *) recognizer
{
    //might need to check if swipe has ended (otherwise not time to handle yet)
    if (recognizer.state != UIGestureRecognizerStateEnded)
        return;

    CGPoint location = [recognizer locationInView:tableView]; //not self.view  
    NSIndexPath *selectedIndexPath = [tableView indexPathForRowAtPoint:location];

    if (selectedIndexPath != nil)
    {
        //user swiped on a tableview cell
    }
    else
    {
        //user did not swipe on a tableview cell
    }
}

self.view!=tableView?谢谢!
recognizer.state!=UIgestureRecognitizerStateEnd
成功了。您知道为什么会发生这种情况吗?识别器会为手势的每个阶段(开始、更改、结束等)触发方法。在您的情况下,位置可能直到结束才有效。