私はカスタムチェックボックスを取り、cellForRowAtIndexPathに次のように記述しました。
static NSString *CustomCellIdentifier = @"CustomCell";
ReportAttendedBy *cell = (ReportAttendedBy *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:@"ReportAttendedBy" owner:self options:nil];
for(id oneObject in nib)
if([oneObject isKindOfClass:[ReportAttendedBy class]])
cell = (ReportAttendedBy *)oneObject;
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textLabel.font= [UIFont fontWithName:@"Arial Rounded MT Bold" size:15.0];
cell.textLabel.textColor=[UIColor whiteColor];
ReportCreateOrView *repVw= [arrAttendedBy objectAtIndex:indexPath.row];
cell.txtCustName.text = repVw.strCustomer;
cell.txtCustName.tag = indexPath.row + 100;
cell.txtCustName.delegate = self;
[cell.txtCustName addTarget:self action:@selector(goAway:) forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.btnCheckBox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.btnCheckBox.tag = indexPath.row + 5000;
NSLog(@"repVw.strSelected=== %@",repVw.strSelected);
if([repVw.strSelected isEqualToString:@"1"])
{
[cell.btnCheckBox.currentBackgroundImage isEqual:[UIImage imageNamed:@"ckeckbox_checked.png"]];
// cell.txtCustName.userInteractionEnabled = true;
}
else
{
[cell.btnCheckBox.currentBackgroundImage isEqual:[UIImage imageNamed:@"ckeckbox_unchecked.png"]];
//cell.txtCustName.userInteractionEnabled = false;
}
return cell;
そして、checkboxClickedメソッドでチェックボックスの値を1と0に維持し、ボタンの画像を変更しています
-(IBAction)checkboxClicked:(id)sender
{
NSLog(@"tag=== %d",[sender tag] - 5000);
int index= [sender tag] - 5000;
UIButton *btn= (UIButton *)sender;
ReportCreateOrView *repObj= [arrAttendedBy objectAtIndex:index];
if([btn.currentBackgroundImage isEqual:[UIImage imageNamed:@"checkbox_unchecked.png"]])
{
[btn setBackgroundImage:[UIImage imageNamed: @"checkbox_checked.png"] forState:UIControlStateNormal];
selCustId = [repObj.strCustId intValue];
val = 1;
[self updateAttendedBy]; //in this method i've written query to update value in table
}
else if([btn.currentBackgroundImage isEqual:[UIImage imageNamed:@"checkbox_checked.png"]])
{
[btn setBackgroundImage:[UIImage imageNamed: @"checkbox_unchecked.png"] forState:UIControlStateNormal];
selCustId = [repObj.strCustId intValue];
val= 0;
[self updateAttendedBy];
}
}
そして、viewWillAppearでgetvaluesへのメソッドを書くことを忘れないでください。:)