インターフェイスのような Twitter iPhone アプリを実装しようとしています。(スワイプして、tableviewcell のビューをカスタム ビューに置き換えます)。スワイプを認識するためにApple を使用しUISwipeGestureRecognizer
ており、 を使用してそのスワイプの開始位置を取得してい[recognizer locationInView:self.view]
ます。これにより CGPoint が得られ、それを で使用してい[tableView indexPathForRowAtPoint:location]
ます。これに関する私の問題は、スワイプが常に、スワイプした実際の行の 1 行上または下で検出されるように見えることです。同じ問題が発生した人はいますか?
編集:私はおそらく、カスタム テーブルビュー セルを使用していることにも言及する必要があります。その高さは、デフォルトのビューよりも大きくなっています。それが違いを生むかどうかはわかりませんが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];
}