I’ve added a button on cell of my table view like this-
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];
and this is my 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;
}
And every thing OK for now, but i want to show an alert when user press checkBoxBtn and if user press OK then the checkBoxAction should called.I know we have UIAlertView delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
But problem is that how to get checkBoxBtn in it?
EDIT: It is OK with Midhun MP’s answer, but i want one more thing- in my cell i have three labels and a button
, with the click on check box i want to change label ‘Due in 6 days’ along with other stuff that we done in my checkBoxAction method
Please help me. Thanks.
For achieving this you need to implement the
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndexdelegate method.You can do it like:
Declare a UIButton instance in .h
like:
Change the button adding method like:
For getting the label:
Add a tag to the label in
cellForRowAtIndexPath:like:And you can get the label using the following code: