我创建了一个名为Checkbox的自定义类,它会在触摸时更改其图像,以便提供复选框效果。但是,当我单击tableView中的一个复选框时,也会选择单独行上的其他复选框。请问我能告诉我代码的问题:
Checkbox.m
- (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, 40, 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;
}
答案 0 :(得分:0)
您正在使用tableView dequeueReusableCellWithIdentifier:CellIdentifier]
。这是一件好事,但这意味着在您的表视图中,您只有尽可能多的单元格。当您滚动并且单元格在一端离开屏幕时,它将被重复使用并从另一端重新进入屏幕。当然,单元格仍然包含相同的复选框,因此如果它在离开屏幕时被检查,它仍将是,如果不是,它将不会。
解决这个问题应该非常简单。在您的tableView:(UITableView *)tableView cellForRowAtIndexPath:
中,您已经根据应该在那里显示的项目填充单元格(lab.text = [dict objectForKey:@"textLabel"];
等等)。因此,您只需要保存tableview中每个项目的复选框状态并将其恢复到那里。
希望有所帮助!