indexPathForRowAtPoint不会给我正确的索引
问题描述:
我试图实现一个像界面一样的推特iPhone应用程序。 (滑动以用自定义视图替换tableviewcell中的视图)。我使用Apple的UISwipeGestureRecognizer
来识别滑动,并且我使用[recognizer locationInView:self.view]
获取了该滑动的起始位置。这给了我一个CGPoint,我用[tableView indexPathForRowAtPoint:location]
。我的问题是,我的滑动似乎总是被检测到在我滑过的实际行的上方或下方一行。有没有人遇到过同样的问题?indexPathForRowAtPoint不会给我正确的索引
编辑:我应该也许还提到我使用自定义tableview单元格,它的高度超过了默认视图。我不确定这是否有所作为,我使用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];
}
答
第一,而不是添加一个手势识别到每个细胞,我建议只需添加一个手势识别为包含表视图全视图(或者,如果这是一个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
}
}
任何机会self.view =的tableView!? – 2010-11-10 19:39:36