我在数据库中存储复选框值(真/假)。我想根据存储在数据库中的值设置复选框选中/取消选中。怎么做 ?我的代码是,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
NSLog(@"Checked arr size %i",[checkedArr count]);
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"white_bg.png"] : [UIImage imageNamed:@"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
[checkedArr removeObjectAtIndex:indexPath.row];
[checkedArr insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage *newImage = (checked) ? [UIImage imageNamed:@"tick.png"] : [UIImage imageNamed:@"white_bg.png"];
cell.accessoryView=[[UIImageView alloc] initWithImage:newImage];
NSLog(@"Val is %i",val);
NSLog(@"selected is %@",[listArray objectAtIndex:indexPath.row]);
}
我使用数组checkedArr 将这些值存储在数据库中。
[dbObj insertQuestData:Question answers:listArray checkVal:checkedArr];
这里 dbObj 是数据库对象,其他是我存储到数据库的值。请让我知道如何实现相同的目标。