2

このようにテーブルビューのセルにボタンを追加しました-

UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal];
    [checkBoxBtn addTarget:self action:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside];
    checkBoxBtn.tag = indexPath.row;
    [cell.contentView addSubview:checkBoxBtn];

これが私のcheckBoxActionです

-(void) checkBoxAction: (UIButton *)sender
{
    NSInteger i =sender.tag + 1;
    float perc = (i*100/18.0);
    NSLog(@"%.2f",perc);
    NSString* percentageStr = [[NSString alloc] initWithFormat:@"%3.2f%%(%d out of 18)",perc, i];
    barTitle.text = percentageStr;
    barFGImage.hidden = NO;
    if (perc == 100.00) {
        [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 276.0, barFGImage.frame.size.height)];
    }
    else
    [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 280*perc/100, barFGImage.frame.size.height)];
    [sender setBackgroundImage:[UIImage imageNamed:@"check_select.png"] forState:UIControlStateNormal];
    sender.userInteractionEnabled = NO;
}

今のところすべてOKですが、ユーザーが押すcheckBoxBtnとアラートを表示したいので、ユーザーがOKを押すと、呼び出されるはずです。デリゲートメソッドcheckBoxActionがあることはわかっています。UIAlertView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

しかし、問題は、checkBoxBtnを取得する方法です。

編集: Midhun MPの回答は問題ありませんが、もう1つ必要です。セルには3つのラベルとボタンこのスクリーンショットのようにがあります。チェックボックスをクリックすると、ラベル「6日以内に期限が切れます」を他のものと一緒に変更します。私のcheckBoxAction方法でやった私を助けてください。ありがとう。

4

1 に答える 1

3

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndexこれを実現するには、デリゲート メソッドを実装する必要があります。

次のようにできます。

次のように .h で UIButton インスタンスを宣言します。

UIButton *button;

ボタンの追加方法を次のように変更します。

UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal];
[checkBoxBtn addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
checkBoxBtn.tag = indexPath.row;
[cell.contentView addSubview:checkBoxBtn];


 - (void) showAlert:(id)sender
{
   button = (UIButton *)sender
   UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Cancel"];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if(buttonIndex == 0)
   {
      [self checkBoxAction:button];
   }
}

ラベルを取得するには:

cellForRowAtIndexPath:次のようにラベルにタグを追加します。

statusText.tag = 7;
[cell.contentView addSubview:statusText];

そして、次のコードを使用してラベルを取得できます。

UILabel *tempLabel = (UIlabel *)[[button superview] viewWithTag:7];
tempLabel.text = @"Midhun";
于 2012-11-23T08:13:39.867 に答える