4

UITableView のカスタム セルにラジオ ボタンを追加し、メソッドを呼び出して画像または背景画像を変更しています。選択したボタンの画像を送信者から設定できますが、他のボタンを元の位置に設定したいので、設定できません。以下の機能は、セル内の適切なラジオ ボタンのように見える必要があるためです。

このメソッドで適切なインデックスパスを取得する際にも問題に直面しています - (void) TableRadioButtonSelection:(id)sender;

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

static NSString *RadioIdentifier = @"RadioCellIdentifier";

        RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier];
        if(myRadiocell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"RadioBtnCell" owner:self options:nil];
            myRadiocell = radioCell;

            //[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            myRadiocell.radioBtn1.tag = ButtonTag;
            //[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
             myRadiocell.radioBtn2.tag = ButtonTag1;
            //[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
             myRadiocell.radioBtn3.tag = ButtonTag2;
            myRadiocell.tag = RadioTag;

            [myRadiocell.radioBtn1 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
            [myRadiocell.radioBtn2 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
            [myRadiocell.radioBtn3 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];

            myRadiocell.backgroundView.backgroundColor = [UIColor clearColor];
            self.radioCell = nil;

        }

return  myRadiocell;

}

- (void) TableRadioButtonSelection:(id)sender {

    //RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview];

    RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
    UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag];
    UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1];
    UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2];

    [mybtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
    [mybtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
    [mybtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];

     //NSLog(@"%@",[cell subviews]);
    UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added.
    UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1];
    UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2];

   // I am unable to set Image of other buttons apart from sender.

    //for(UIView *item in [mysubview subviews]) {

        if ([mysubview isKindOfClass:[UIButton class]]) {
            UIButton *newBtn = (UIButton*)mysubview;
            UIButton *newBtn1 = (UIButton*)mysubview1;
            UIButton *newBtn2 = (UIButton*)mysubview2;

            NSLog(@"OK %@",newBtn);
            //[newBtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [newBtn setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [newBtn1 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [newBtn2 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];

        }
    //}

    NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell]; 
    NSLog(@"%d",indexPath.row);

    //  Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image

    UIButton *btn = (UIButton *)sender;
    [btn setImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal];
    //[btn setBackgroundImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal];
}
4

1 に答える 1

0

この行をこれに変更してみてください;-

RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];

to

     RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*];

このようにして、テーブル ビュー セルにアクセスできます。その後、セル コンテンツ ビューからボタンを取得し、さらにコーディングを行うことができます。

indexPathがわからない場合は、テーブルビューのrowNumberからindexPathを作成できるとします(テーブルの行番号をRadioTagに保存したと思います。そうであれば、このフィールドをパラメーターとして使用できます)。

于 2012-09-04T06:38:04.350 に答える