选中一个复选框时,选中其他复选框

问题描述

| 我创建了一个名为CheckBox自定义类,该类可在触摸时更改其图像,从而提供复选框效果。但是,当我单击tableView中的一个复选框时,也会选择单独行上的其他复选框。请您告诉我代码的问题: 复选框
- (void)checkImages {
    NSUInteger tag = [self tag];
    BOOL val = [[NSUserDefaults standardUserDefaults] boolForKey:[Nsstring stringWithFormat:@\"%i\",tag]];
    if (val == YES) {
        [self setimage:[UIImage imageNamed:@\"checkBox-pressed.png\"] forState:UIControlStatenormal];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[Nsstring stringWithFormat:@\"%i\",tag]];
    }
    else if (val == NO) {
        [self setimage:[UIImage imageNamed:@\"checkBox.png\"] forState:UIControlStatenormal];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[Nsstring stringWithFormat:@\"%i\",tag]];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([[[[event alltouches] anyObject] view] tag] == [self tag]) {
        [self checkImages];
    }
}
RootViewController:
- (void)viewDidLoad
{


    //This will set a solid background color
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    if (_checkBoxArray == nil) {
        [self setItemArray:[[NSMutableArray alloc] init]];
    }
    if (_cellTextArray == nil) {
        [self setCellTextArray:[[NSMutableArray alloc] init]];
    }
    if (![[NSUserDefaults standardUserDefaults] boolForKey:kFL]) {
        NSMutableArray *custArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < [_checkBoxArray count]; i ++) {
            CheckBox *c = (CheckBox *)[_checkBoxArray objectAtIndex:i];
            [c setimage:[UIImage imageNamed:@\"checkBox.png\"] forState:UIControlStatenormal];
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[Nsstring stringWithFormat:@\"%i\",[c tag]]];
            [custArr addobject:c];
        }
        [_checkBoxArray release];
        [_checkBoxArray setArray:custArr];
        [custArr release];

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFL];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
 for (int i = 0; i < [textLabelArray count]; i++) {
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:[textLabelArray objectAtIndex:i],[detailTextArray objectAtIndex:i],[cornLabelArray objectAtIndex:i],nil] forKeys:[NSArray arrayWithObjects:@\"textLabel\",@\"detailTextLabel\",@\"cornerLabel\",nil]];
        [_cellTextArray addobject:dict];
        [dict release];
    }
    for (int i = 0; i < [_cellTextArray count]; i++) {
        CheckBox*btn = [[CheckBox alloc] init];
        [btn setFrame:CGRectMake(0,10,40,40)];
        [btn setTag:i];
        UIImage *img = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] boolForKey:[Nsstring stringWithFormat:@\"%i\",btn.tag]] ? @\"checkBox.png\":@\"checkBox-pressed.png\"];
        [btn setimage:img forState:UIControlStatenormal];
        [_checkBoxArray addobject:btn];
        [btn release];
    }
    [self.tableView reloadData];
    [super viewDidLoad];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static Nsstring *CellIdentifier = @\"CustomCell\";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    CheckBox *btn;
    UILabel *lab,*dlabl,*cornerLabel;
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        btn = (CheckBox *)[_checkBoxArray objectAtIndex:indexPath.row];
        [cell.contentView addSubview:btn];
        //I added this code:
        cell.textLabel.font = [UIFont fontWithName:@\"Helvetica\" size:12.0];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 3; // 0 means no max.


        UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"gradient7.png\"]] autorelease];
        [cell setBackgroundView:img];

        lab = [[[UILabel alloc] initWithFrame:CGRectMake(40,18,cell.contentView.frame.size.width-15,22)] autorelease];
        [lab setBackgroundColor:[UIColor clearColor]];
        [lab setTextColor:[UIColor whiteColor]];
        [lab setAdjustsFontSizetoFitWidth:YES];
        [lab setTextAlignment:UITextAlignmentLeft];
        [lab setTag:kTEXT_LABEL_TAG];
        [cell.contentView addSubview:lab];

        dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5,54,cell.contentView.frame.size.width- 1,22)] autorelease];
        [dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]];
        [dlabl setBackgroundColor:[UIColor clearColor]];
       // [dlabl setAdjustsFontSizetoFitWidth:YES];
        [dlabl setTextAlignment:UITextAlignmentLeft];
        [dlabl setTag:kDETAIL_TEXT_LABEL_TAG];
        [dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]];
        [cell.contentView addSubview:dlabl];

        cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40,19,20)] autorelease];
        [cornerLabel setTextColor:[UIColor whiteColor]];
        //[cornerLabel setFont:[UIFont systemFontOfSize:12]];
        [cornerLabel setAdjustsFontSizetoFitWidth:YES];
        [cornerLabel setBackgroundColor:[UIColor clearColor]];
        [cornerLabel setTextAlignment:UITextAlignmentCenter];
        [cornerLabel setTag:kCORNER_TEXT_LABEL_TAG];
        [cell.contentView addSubview:cornerLabel];
        [cornerLabel setAdjustsFontSizetoFitWidth:YES];
    }
    else {
        lab = (UILabel *)[[cell contentView] viewWithTag:kTEXT_LABEL_TAG];
        dlabl = (UILabel *)[[cell contentView] viewWithTag:kDETAIL_TEXT_LABEL_TAG];
        cornerLabel = (UILabel *)[[cell contentView] viewWithTag:kCORNER_TEXT_LABEL_TAG];
        btn = (CheckBox *)[[cell contentView] viewWithTag:kBTN_TAG];
    }
    NSDictionary *dict = [_cellTextArray objectAtIndex:indexPath.row];
    lab.text = [dict objectForKey:@\"textLabel\"];
    dlabl.text = [dict objectForKey:@\"detailTextLabel\"];
    cornerLabel.text = [dict objectForKey:@\"cornerLabel\"];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:[Nsstring stringWithFormat:@\"%d\",indexPath.row]] == NO) {
        [btn setimage:[UIImage imageNamed:@\"checkBox-pressed.png\"] forState:UIControlStatenormal];
    }else {
        [btn setimage:[UIImage imageNamed:@\"checkBox.png\"] forState:UIControlStatenormal];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80.0f;
}
    

解决方法

您正在使用
tableView dequeueReusableCellWithIdentifier:CellIdentifier]
。这是一件好事,但这意味着在表视图中,您只能看到尽可能多的单元格。当您滚动并且某个单元格离开屏幕的一端时,它将被重用并从另一端重新进入屏幕。当然,该单元格仍包含相同的复选框,因此,如果它在离开屏幕时被选中,则它仍将是,否则将不会。 解决这个问题应该很简单。在
tableView:(UITableView *)tableView cellForRowAtIndexPath:
中,您已经按照应在此处显示的项目(
lab.text = [dict objectForKey:@\"textLabel\"];
等)填充了单元格。因此,您要做的就是保存表视图中每个项目的复选框状态并将其恢复到那里。 希望有帮助!