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;
}

现在一切正常,但我想在用户按下时显示警报checkBoxBtn,如果用户按下 OK,那么checkBoxAction应该调用。我知道我们有UIAlertView委托方法

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

但问题是如何在其中获取 checkBoxBtn ?

编辑: Midhun MP 的回答没问题,但我还想要一件事——在我的单元格中,我有三个标签和一个按钮像这个屏幕截图,单击复选框我想更改标签“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 回答