gpt4 book ai didi

iphone - indexPathForRowAtPoint 不会给我正确的索引

转载 作者:行者123 更新时间:2023-12-03 20:21:15 25 4
gpt4 key购买 nike

我正在尝试实现一个类似 twitter iPhone 应用程序的界面。 (滑动即可用自定义 View 替换 tableviewcell 中的 View )。我使用 Apple 的 UISwipeGestureRecognizer 来识别滑动,并使用 [recognizer locationInView:self.view] 获取该滑动的起始位置。这给了我一个 CGPoint,我将其与 [tableView indexPathForRowAtPoint:location] 一起使用。我的问题是,我的滑动似乎总是在我滑动的实际行上方或下方的一行被检测到。有人遇到过同样的问题吗?

编辑:我可能还应该提到我正在使用自定义表格 View 单元格,并且它的高度大于默认 View 。我不确定这是否有任何区别,我使用 heightForRowIndexAtPath: 返回高度。

我的代码是 -

- (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];
}

最佳答案

首先,我建议不要向每个单元格添加手势识别器,而是建议仅向包含表格 View 的整个 View 添加一个手势识别器(或者如果这是一个 UITableViewController,那么它们是同一件事)。

在viewDidLoad中:

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] 
initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

然后,在handleSwipe方法中:

- (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
}
}

关于iphone - indexPathForRowAtPoint 不会给我正确的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4148264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com