如何在表格视图单元格上获取按钮动态按钮的按钮单击事件

时间:2012-08-11 05:14:36

标签: iphone ios uitableview

我正在使用每个表格视图单元格中具有动态按钮数功能的应用程序。按钮数按行顺序变化。

我想给图片一个按钮来点击。但我无法得到它所以任何人都可以帮助我改变所选按钮的图像。

1 个答案:

答案 0 :(得分:2)

您应该在yourButton中添加标记,并在indexPath.row方法中为其指定- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [yourButton setTitle:@"Button" forState:UIControlStateNormal];
    [yourButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    yourButton.tag=indexPath.row;
    yourButton.frame = CGRectMake(5, 5, 100, 30);     
    [cell addSubview:yourButton];

之后,定义buttonSelected:方法。

-(void)buttonSelected:(id)sender{

    UIButton *button = (UIButton *)sender;
    NSLog(@"buttonSelectd: %d",button.tag);
   //implement your code what you want.

}

我认为这会对你有所帮助。

相关问题