uitableview触摸事件

时间:2012-06-22 11:53:58

标签: iphone objective-c

我在UITableViewCell内创建了一个按钮,当我点击一个按钮时,我想要使用索引值来创建视图控件。

 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];

3 个答案:

答案 0 :(得分:1)

您应该使用indexPath.row标记按钮。

tapped.tag = indexPath.row;

在您的事件处理代码中,您应该使用该标记来查找索引。

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

答案 1 :(得分: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];

答案 2 :(得分:0)

您可以按如下方式标记按钮:

tapped.tag = indexPath.row

在tapped方法中你可以按如下方式使用它:

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

}

根据您的要求使用此索引......