我正在尝试实现一个类似界面的 twitter iPhone 应用程序。(滑动以将 tableviewcell 中的视图替换为自定义视图)。我正在使用 AppleUISwipeGestureRecognizer
来识别滑动,并且我正在使用[recognizer locationInView:self.view]
. 这给了我一个 CGPoint,我将它与[tableView indexPathForRowAtPoint:location]
. 我的问题是,我的滑动似乎总是在我刷入的实际行上方或下方的一行被检测到。有没有人遇到过同样的问题?
编辑:我可能还应该提到我正在使用自定义 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];
}