0

我在里面创建了一个按钮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];
4

3 回答 3

1

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

tapped.tag = indexPath.row;

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

-(void) tapped:(UIButton *)sender
{
    UIButton *btn = (UIButton *)sender;
    int index = btn.tag;
    //Do rest...
}
于 2012-06-22T11:55:26.287 回答
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];
于 2012-06-22T11:59:34.350 回答
0

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

tapped.tag = indexPath.row

而在 tapped 方法中,您可以按如下方式使用它:

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

}

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

于 2012-06-22T12:00:44.897 回答