2014-03-14 30 views
0

我試圖找到UITableViewCell當用戶按UIButton這是一個subview單元格的行。使用下面的代碼,我可靠地得到比實際的indexpath少一個。任何人都知道爲什麼這是/如果這是在iOS7中使用subview(在本例中爲UIButton)找到單元的indexpath的好方法?在uitableviewcell中的uibutton給我的indexpath小於實際的indexpath iOS7

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:_messageTableView]; 
NSIndexPath *indexPath = [_messageTableView indexPathForRowAtPoint:buttonPosition]; 
if (indexPath != nil) 
{ 
    NSLog(@"highlightedcell: %i",indexPath.row); 
} 
+0

將'CGPointZero'改爲'CGPointMake(5,5)',你應該得到更好的結果。 – rmaddy

回答

0

設置按鈕標籤的ccorrect索引,並獲得點擊按鈕的索引路徑。

[yourButton setTag:indexPath.row]; 
[yourButton addTarget:self action:@selector(yourButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 

-(void)yourButtonClick:(id)sender 
{ 
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:yourTableView]; 
    NSIndexPath *deliverIndPath = [yourTableView indexPathForRowAtPoint:buttonPosition]; 
    NSLog(@"deliverIndPath : %@",deliverIndPath); 
} 
+0

設置標籤與獲取indexpath有什麼關係? – Apollo

+0

如果您在tableview中創建多個按鈕,然後將indexpath.row設置爲按鈕標記,並通過點擊按鈕(id)發件人,您將獲得該點擊的按鈕行的索引路徑。 DeliveryIndPath是indexpath。 –

0

按我的建議執行以下操作:

設置按鈕標籤如下indexPath.row:

cell.button.tag = indexPath.row; 

現在像下面的示例代碼按鈕添加一個選擇:

[cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

現在執行選擇器並找到所選行的索引,如下所示:

- (void)buttonClicked:(id)sender {  
     UIButton *btnTemp=(UIButton *)sender; 
     NSLog(@"%d",btnTemp.tag); 
} 

所以在btnTemp.tag你可以找到所選單元格

相關問題