Ios UITableView从数据库值设置复选框状态

Ios UITableView从数据库值设置复选框状态,ios,sqlite,uitableview,checkbox,Ios,Sqlite,Uitableview,Checkbox,我正在数据库中存储复选框值(真/假)。我想根据数据库中存储的值设置复选框checked/unchecked。怎么做?我的密码是 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row

我正在数据库中存储复选框值(真/假)。我想根据数据库中存储的值设置复选框checked/unchecked。怎么做?我的密码是

- (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]);

}
我使用数组checkedar将这些值存储在数据库中

[dbObj insertQuestData:Question answers:listArray checkVal:checkedArr];
这里dbObj是数据库对象,其他的是我存储到数据库中的值。
请让我知道如何实现相同的功能。

列表数组和选中数组应该是可变数组,发送给数据库函数,该函数将填充数据

    self.listArray = [NSMutableArray array];
    self.checkedArray = [NSMutableArray array];

    [dbObj insertQuestData:Question answers:self.listArray checkVal:self.checkedArray];
//    [self insertList:self.listArray andChecks:self.checkedArray];
insertQuestData方法应该像这样填充数据-

- (void) insertList:(NSMutableArray *)list andChecks:(NSMutableArray *)checks
{
    for (int i = 0; i < 100; i++) {
        [list addObject:[NSString stringWithFormat:@"item %d", i]];
        BOOL checkValue = (i % 4) == 0 ? YES : NO;
        [checks addObject:[NSNumber numberWithBool:checkValue]];
    }
}
这是我的示例代码在iPhone上显示的-[每第四行(绿色)勾选/选中,其他行为灰色/未选中]


如果您有任何具体问题,请随时提问

如何将这些值设置为复选框,以便根据数据库中的值(真/假),复选框保持选中/未选中状态。因此,当我从一个视图移动到另一个视图并返回时,这些复选框将保持其状态?cell.accessoryView显示复选框图像(勾选/白色)。(检查最后3行)。只要您的self.checkedArray(驱动复选框状态的数据),您就应该是好的。请记住,无论何时数据有任何更新,都应该使用
[tableview reloadData]
刷新表。非常感谢。这就是我的工作。但是,如何根据bool设置复选框呢?在哪里放置代码?在CellForRowatineXpath或DidSelectRowatineXpath?中,通常,
CellForRowatineXpath
方法应该是布局单元格视图的唯一位置。任何需要更改布局的操作,都应该更改其中的数据源(self.checkedArray),然后调用
[self.tableview reloadData]
,或者使用
reloadRowsAtIndexPaths
有选择地刷新某些单元格(请参见此处的开发人员文档-:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"CellIdentifier";//[NSString stringWithFormat:@"Cell%i",indexPath.row];
    UITableViewCell *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=[self.listArray objectAtIndex:indexPath.row];
    NSLog(@"Checked arr size %i",[self.checkedArray count]);
    BOOL checked =  [[self.checkedArray objectAtIndex:indexPath.row] boolValue];
    UIImage *image = (!checked) ? [UIImage imageNamed:@"white_bg.png"] : [UIImage imageNamed:@"tick.png"];
    cell.accessoryView = [[UIImageView alloc] initWithImage:image];

    return cell;    
}