2013-07-23 78 views
1

我需要在iOS中獲取索引UITableViewUILongPressGestureRecognizeriOS中的UITableView indexPathForSelectedRow始終返回0

首先,我添加了UILongPressGestureRecognizer,並使用以下代碼檢索所選行的索引路徑。

- (void)handleLongPress : (UILongPressGestureRecognizer *)ges 
{ 
    NSIndexPath *indexPath = [self.tblMainTableView indexPathForSelectedRow]; 
    NSLog(@"%i",indexPath.row); 
} 

當我對UITableView第三排攻,它只返回0並始終返回0無論我上的tableView挖掘。

我怎樣才能得到UILongPressGestureRecognizer索引tableview?

感謝您的任何建議和幫助。

+0

是你tblMainTableView IBOutlet中勾搭上了?如果它沒有,那就是爲什麼你總是得到0。 –

回答

3
-(void)handleLongPress:(UILongPressGestureRecognizer *)ges 
{ 
    CGPoint touch = [ges locationInView:self.tblMainTableView]; 

    NSIndexPath *indexPath = [self.tblMainTableView indexPathForRowAtPoint:touch]; 

} 

使用上的tableView觸摸點通過indexPathForRowAtPoint方法來訪問該行

1

我懷疑你的行沒有得到選擇,因爲你沒有「潤色」,直到很久以後壓它如果你點擊它然後按下,可能會起作用,但這會破壞目的。你可以用它來獲取長按點:

CGPoint pressPoint = [ges locationInView]; 

然後用此得到該點的細胞:

indexPath path = [self.tblMainTableView indexPathForRowAtPoint:pressPoint];