2012-06-22 306 views
0

我在UITableViewCell裏面創建了一個按鈕,當我點擊一個按鈕時,我想用作索引值的作者視圖控件。uitableview觸摸事件

tapped = [UIButton buttonWithType:UIButtonTypeCustom]; 

[tapped setFrame:CGRectMake(0, 0, 320, 100)]; 
[tapped setTitle:@"button" forState:UIControlStateNormal]; 

NSInteger tet; 

tet = indexPath.row; 
[tapped addTarget:self action:@selector(tapped:) forControlEvents: UIControlEventTouchDown]; 

[Cell addSubview:tapped]; 
+0

[其中的UIButton在一個UITableView壓制檢測]可能重複( http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) – Vladimir

回答

1

你應該用indexPath.row標記一個按鈕。

tapped.tag = indexPath.row; 

在您的事件處理代碼中,您應該使用該標記來查找索引。

-(void) tapped:(UIButton *)sender 
{ 
    UIButton *btn = (UIButton *)sender; 
    int index = btn.tag; 
    //Do rest... 
} 
+0

謝謝,但我想索引path.row值 – dhaya

+0

btn.tag將完全等於indexPath.row ... – Rajkumar

+0

你這麼好我得到了答案非常感謝... – dhaya

0

試試這個

-(void) tapped:(UIButton *)sender 
{ 
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; 
    NSIndexPath *indexPath = [myTableView indexPathForCell:clickedCell]; 
    int section = indexPath.section; 
    // You get easily your index path row 
    int row = indexPath.row; 
    // push your controller 

} 

第一次更新我們的代碼

使用烏爾事件UIControlEventTouchUpInside

tapped = [UIButton buttonWithType:UIButtonTypeCustom]; 

[tapped setFrame:CGRectMake(0, 0, 320, 100)]; 
[tapped setTitle:@"button" forState:UIControlStateNormal]; 

NSInteger tet; 

tet = indexPath.row; 
[tapped addTarget:self action:@selector(tapped:) forControlEvents: UIControlEventTouchUpInside]; 

[Cell addSubview:tapped]; 
+0

使用沒有「標籤」 – dayitv89

+0

tblMyCard是什麼意思 – dhaya

+0

這是UItableView對象 – dayitv89

0

你可以按照如下標籤上的按鈕:

tapped.tag = indexPath.row 

而在螺紋的方法,您可以按如下方式使用它:

-(IBAction)tapped:(id)sender 
{ 
UIButton *btn = (UIButton *)sender; 
int index = btn.tag; 

} 

使用該指數按您的要求......

+0

謝謝,但我想索引path.row值 – dhaya

+0

這裏索引將完全等於indexPath.row ... – Rajkumar