2013-10-18 23 views
2

我有一個自定義的方法來檢測單元格的圖像上的一個水龍頭。我也想找到圖像相關單元的索引路徑,並在函數內使用它。這裏是我使用的是什麼:indexView的tableViewCell

的cellForRowAtIndexPath:

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellImageTapped:)]; 
tapped.numberOfTapsRequired = 1; 
[cell.imageView addGestureRecognizer:tapped]; 

方法,我嘗試在獲得索引路徑:

-(void)cellImageTapped:(id)sender { 
    if(videoArray.count > 0){ 
     Video *currentVideo = [videoArray objectAtIndex:INDEX_PATH_OF_CELL_IMAGE]; 
    //do some stuff   
    } 
} 

我不知道如何通過索引路徑。有任何想法嗎?

回答

1

在您的UITableViewDataSourcetableView:cellForRowAtIndexPath:方法中爲UIImageView添加標籤。

cell.imageView.tag = indexPath.row; 
+0

我其實,我只是沒有列出來,但我沒有我的函數中獲得細胞。 –

+0

您可以訪問imageView。在cellImageTapped:方法中使用(((UITapGestureRecognizer *)sender).view.tag。輕擊手勢識別器的視圖是被點擊的imageView。 – paulrehkugler

+0

這是假設在tableView中只有一個部分 – StevenTsooo

1

使用委託方法didSelectRowAtIndexPath方法:方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self cellImageTapped:indexPath]; 
} 

然後您可以將索引傳遞到即

-(void)cellImageTapped:(NSIndexPath *)indexPath 
{ 
    Video *currentVideo = [videoArray objectAtIndex:indexPath.row]; 
} 
+0

didSelectRowAtIndexPath在我點擊單元格時未被調用。否則,這將是完美的:( –

1

我結束了使用發件人的視圖的標籤的功能。希望這會幫助某人,因爲我浪費了一小時才找到答案。

-(void)cellImageTapped:(id)sender { 

UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender; 

      if(videoArray.count > 0){ 
       NSInteger datIndex = gesture.view.tag; 
       Video *currentVideo = [videoArray objectAtIndex:datIndex]; 
      } 

} 
4

我會推薦這種方式來獲取其定製子視圖細胞indexPath - (與iOS 7以及以前所有版本兼容)

- (void)cellImageTapped:(UIGestureRecognizer *)gestureRecognizer 
{ 
    UIView *parentCell = gestureRecognizer.view.superview; 

    while (![parentCell isKindOfClass:[UITableViewCell class]]) { // iOS 7 onwards the table cell hierachy has changed. 
     parentCell = parentCell.superview; 
    } 

    UIView *parentView = parentCell.superview; 

    while (![parentView isKindOfClass:[UITableView class]]) { // iOS 7 onwards the table cell hierachy has changed. 
     parentView = parentView.superview; 
    } 


    UITableView *tableView = (UITableView *)parentView; 
    NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)parentCell]; 

    NSLog(@"indexPath = %@", indexPath); 
} 
6

簡單的方法:

  • 獲取觸摸點

  • 然後在點獲得細胞的指數路徑

的代碼是:

-(void)cellImageTapped:(id)sender { 
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender; 
    CGPoint point = [tap locationInView:theTableView]; 

    NSIndexPath *theIndexPath = [theTableView indexPathForRowAtPoint:point]; 

    if(videoArray.count > 0){ 
     Video *currentVideo = [videoArray objectAtIndex:theIndexPath]; 
     //do some stuff 
    } 
} 
+0

適用於iOS 7.1 – JimVision

+0

這是最好的答案。 – sabiland