2012-08-11 73 views

回答

2

您应该为您的按钮添加标签,并在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中为其分配indexPath.row

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. 

} 

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

+0

感谢prasad为你的答案我知道它工作正常,但我的问题是每个单元格上的按钮数是不同的手段(单元格1有2个按钮,单元格2 6按钮)按钮的数量不固定,所以我有麻烦,这样做这里是我的示例代码添加按钮单元格 – user1579258 2012-08-17 05:01:08

+0

for(int i = 0; i <[Array count]; i ++) { optionButton = [[UIButton alloc] init]; optionButton = [UIButton buttonWithType:UIButtonTypeCustom]; optionButton.frame = CGRectMake(5,80,22,18); [optionButton addTarget:self action:@selector(buttonPresssed :) forControlEvents:UIControlEventTouchUpInside]; optionButton.tag = i; [cell addSubview:optionButton]; } – user1579258 2012-08-17 05:07:58

+0

按钮的数量取决于阵列并发,所以我无法追踪哪个按钮被点击,还有一件事我必须一起收集选择按钮值,所以请你可以告诉我如何做到这一点。预先感谢 – user1579258 2012-08-17 05:09:55

相关问题