我采用了自定义复选框,并在 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 中编写获取值的方法。:)