2012-06-22 330 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