2013-04-10 12 views
0

我有一個UITableViewCell,我在其中創建一些按鈕(它們的作用類似單選按鈕)並將標籤分配給按鈕。但標籤不是靜態的。他們是動態的。現在,當點擊一個特定的單選按鈕時,我需要用一些圖像(radio_On.png)設置該按鈕,並將所有按鈕保留爲(radio_Off.png)。但爲了設置圖像,我無法理解如何獲取特定單元中所有按鈕的標記值,因爲它們不是靜態的。在單獨的方法中獲取UITableViewCell的控件(UIButtons)和它們的標籤

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height+3,17, 17)]; 
    [btTemp1 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    [btTemp1 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal]; 
    [btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f]; 

    btTemp1.tag=++Optionid; 
    [hlcell.contentView addSubview:btTemp1]; 

    btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,17, 17)]; 
    [btTemp2 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    [btTemp2 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal]; 
    [btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f]; 

    btTemp2.tag=++Optionid; 
    [hlcell.contentView addSubview:btTemp2]; 
} 

-(IBAction) radioButtonClicked:(UIButton *) sender { 
    UIButton *button = (UIButton *)sender; 
    NSLog(@"%d", [button tag]); 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) 
    [[sender superview] superview]]; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section]; 

    [sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal]; 
} 

在這裏,人們radiobuttton當我的TableView細胞檢查其他按鈕是沒有得到釋放。

編輯:

Prevoiusly我給這個代碼:

-(IBAction) radioButtonClicked:(UIButton *) sender { 
     UIButton *button = (UIButton *)sender; 
     NSLog(@"%d", [button tag]); 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) 
     [[sender superview] superview]]; 

     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section]; 

UIButton *btnTemp1 = (UIButton*)[cell viewWithTag:10]; //Static .But now I want dynamic 
     UIButton *btnTemp2 = (UIButton*)[cell viewWithTag:11];//Static .But now I want dynamic 
     UIButton *btnTemp3 = (UIButton*)[cell viewWithTag:12];//Static .But now I want dynamic 
     UIButton *btnTemp4=(UIButton *)[cell viewWithTag:13];//Static .But now I want dynamic 
     UIButton *btnTemp5=(UIButton *)[cell viewWithTag:14];//Static .But now I want dynamic 

     [radioButtonsinaSection addObject:btnTemp1]; 
     [radioButtonsinaSection addObject:btnTemp2]; 
     [radioButtonsinaSection addObject:btnTemp3]; 
     [radioButtonsinaSection addObject:btnTemp4]; 
     [radioButtonsinaSection addObject:btnTemp5]; 


    } 


    for(int i=0;i<[radioButtonsinaSection count];i++){ 

     [[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal]; 

    } 
    [sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal]; 
    } 

我怎樣才能得到一個細胞的所有控件(按鈕及其標籤),其中按鈕(單選)被點擊?

回答

1

按我的知識,你錯過radioButtonClicked方法,它跟蹤哪些按鈕clicked..you試試這個代碼迴路..

(IBAction) radioButtonClicked:(UIButton *) sender{ 

       for(int i=0;i&amp;lt;[self.radioButtons count];i++){ 
         [[self.radioButtons objectAtIndex:i] setImage:[UIImage  
            imageNamed:@&quot;radio-off.png&quot;] 
             forState:UIControlStateNormal]; 
       } 
       [sender setImage:[UIImage imageNamed:@&quot;radio-On.png&quot;] 
            forState:UIControlStateNormal]; 
} 

我認爲它可以幫助你,如果不是隨意再問..

享受..

+0

u能告訴我在哪裏被self.radioButtons創造的呢?我想這是數組告訴任何單選按鈕的一個部分......但是,如何創建它在哪裏? – Honey 2013-04-10 08:37:17

+0

@arizah radioButtons已在class..not中創建..而不是你的class.so ..h和.m中的方法..試試吧..&讓我知道.. – Shivaay 2013-04-10 08:41:08

+0

對不起,我沒有得到你..我在.h本身中創建了按鈕(單選按鈕),並在cellforrowatindexpath中使用它們。但是如何獲得特定節的單選按鈕(例如4或5)(帶有標籤:不是靜態的)?在這種方法中:(IBAction)radioButtonClicked :(UIButton *)sender { ? – Honey 2013-04-10 08:45:58

相關問題