2012-12-08 46 views
1

我在手勢識別器選擇器中接收正確的NSIndexPath有問題。 在UITableViewController:viewDidLoad IM初始化手勢識別TableView indexPathForRowAtPoint返回行索引0

UISwipeGestureRecognizer* rightSwipeGestureRecognizer = 
     [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)]; 
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
[self.tableView addGestureRecognizer:rightSwipeGestureRecognizer]; 

在手勢處理機IM試圖讓行索引:

- (void) swipeRight: (UISwipeGestureRecognizer *)gestureRecognizer 
{ 
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
    CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; 
    NSInteger row = swipedIndexPath.row; 
    NSLog(@"Row %d", swipedIndexPath.row); 
} 

CGPoint是正確的,但行始終爲0。任何想法,爲什麼?

+0

是tableView有效,可見並填充行? –

+0

是的,它是有效的,因爲如果我調用self.tableView:setEditing我得到預期的行爲(每個單元格獲取「 - 」按鈕刪除)。 – Gossamer

+1

你有多個部分嗎?每個部分有多少行?還記錄swipedIndexPath.section。 – Anna

回答

0

添加UISwipeGestureRecognizer每個的UITableViewCell(具有相同的選擇)。然後,當選擇器被調用時,您可以通過以下方式獲取單元格:sender.view

+0

如果我不知道如何通過tableview生病嘗試你的建議。 – Gossamer

+0

你知道了嗎?我有類似的問題。如果我滑過一個空單元格,它將返回「0」索引,這是一個有效的indexPath.row,但是是錯誤的。 – addzo