0

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

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

4

1 回答 1

1

The list array and checked array should be mutable array sent to database function which fills up the data.

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

    [dbObj insertQuestData:Question answers:self.listArray checkVal:self.checkedArray];
//    [self insertList:self.listArray andChecks:self.checkedArray];

The insertQuestData methods should fill up data like this -

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

The table datasource methods should be like this - (notice the commented line as correction to your code)

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

This is what my sample code show on iPhone - [every 4th row is (green)ticked/selected and others are grey/unselected]

enter image description here

Feel free to ask if you have any specific questions / issues you face.

于 2013-10-19T05:53:55.160 回答