2012-08-11 74 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

相關問題