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

时间:2013-10-19 05:14:36

标签: ios sqlite uitableview checkbox

我在数据库中存储复选框值(true / false)。我想根据存储在数据库中的值设置复选框选中/取消选中。怎么做 ?我的代码是,

- (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是数据库对象,其他是我存储到数据库的值。 请让我知道如何实现同样的目标。

1 个答案:

答案 0 :(得分:1)

list array和checked数组应该是可变数组,发送到数据库函数,填充数据。

    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]];
    }
}

表数据源方法应该是这样的 - (注意注释行作为代码的更正)

- (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;    
}

这是我的示例代码在iPhone上显示的内容 - [每隔4行(绿色)勾选/选中,其他行为灰色/未选中]

enter image description here

随时问您是否有任何具体问题/问题。

相关问题