我有一个复选框类,我想将布尔核心数据属性从NO
切换到YES
。绘制实际的复选框并使其切换不是问题,但坚持其状态是。该复选框位于我的堆栈中的第二个视图控制器中(RVC
< - > ItemsVC
(复选框在这里)< - > ItemDetailVC
)。如果我切换复选框然后以任一方式导航,状态将返回未选中状态。
我已经尝试了几种方法来a)使复选框状态保持不变并且b)使复选框的状态将我的布尔属性的状态从NO
更改为YES
。
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
[self addTarget:self action: @selector(checkboxTapped) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (IBAction) checkboxTapped {
if (self.isChecked == NO) {
self.isChecked = YES;
[self setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
self.selectedItem.inCart = isChecked;
} else {
self.isChecked = NO;
[self setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
}
isChecked
是在标题中声明的BOOL,selectedItem.inCart
分别是我的核心数据实体和布尔属性。
编辑:以下是ItemsVC
中显示复选框的代码。
- (UITableViewCell *)tableView:(UITableView *)IVCTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [IVCTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Item *item = [fetchedResultsController_ objectAtIndexPath:indexPath];
[self configureCell:cell withItem:item];
UILabel *itemLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 100, 40)];
[cell.contentView addSubview:itemLabel];
itemLabel.text = item.itemName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Checkbox *checkbox = [[Checkbox alloc] initWithFrame:CGRectMake(10, 0, 70, 50)];
[cell.contentView addSubview:checkbox];
return cell;
}
编辑2:这是我的代码的当前状态,其中包含有关哪些有效,哪些无效的注释。此时,复选框将检查并取消选中,但检查将每10行重复一次,我认为这是dequeueReusableCellWithIdentifier
正在做的事情。
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// If I don't set these images here, they don't appear in the table
[self setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
[self addTarget:self action: @selector(checkboxTapped) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
// This function prevents checked checkboxes from unchecking
/*
- (void)setIsChecked:(BOOL)isChecked {
if (isChecked == isChecked_)
return;
[self setImage:[UIImage imageNamed:isChecked_?@"checked.png":@"unchecked.png"] forState:UIControlStateNormal];
self.selectedItem.inCart = [NSNumber numberWithBool:isChecked_];
}
*/
- (IBAction) checkboxTapped {
if (self.isChecked == NO) {
self.isChecked = YES;
// If I comment this out, the checked.png doesn't render
[self setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
self.selectedItem.inCart = [NSNumber numberWithBool:isChecked_];
// I commented this out because for some reason it prevents the checkbox from unchecking
//self.isChecked = [[selectedItem_ inCart] boolValue];
} else {
self.isChecked = NO;
// If I comment this out, once the checkbox is checked, it won't uncheck
[self setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
self.selectedItem.inCart = [NSNumber numberWithBool:isChecked_];
self.isChecked = [[selectedItem_ inCart] boolValue];
}
}
我将cellForRowAtIndexPath
留给@imaginaryboy建议,除了我使用checkBox.isChecked = [[item inCart] boolValue];
作为@westsider建议。虽然这看起来比我以前做的更干净,但我不太确定如何处理我的原始问题。我一直在重新阅读文档,但仍然不知所措。有没有办法检查inCart
实际上是否改变了状态?对于已检查的单元格,我应该缓存它们,还是将单元格写入plist,或者在创建时为每个单元格赋予其自己的唯一标识符(正如我写的那样,这似乎是要走的路。是吗?)?< / p>
我觉得如果我真的成功地改变了inCart的状态(这是一个核心数据属性),那么在选中复选框时不应该自动保存它的状态,假设checkBox.isChecked = [[item inCart] boolValue];
有效?
答案 0 :(得分:2)
首先,正如我在不久前评论过的那样,除了第一次创建单元格之外,每次重复使用单元格时,都会重复使用单元格,但会添加新的UILabel
和Checkbox
。
要对此进行排序,请执行以下操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *itemLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 100, 40)];
itemLabel.tag = 1;
[cell.contentView addSubview:itemLabel];
Checkbox *checkbox = [[Checkbox alloc] initWithFrame:CGRectMake(10, 0, 70, 50)];
checkbox.tag = 2;
[cell.contentView addSubview:checkbox];
}
Item *item = [fetchedResultsController_ objectAtIndexPath:indexPath];
[self configureCell:cell withItem:item];
UILabel *itemLabel = (UILabel*)[cell viewWithTag:1];
itemLabel.text = item.itemName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Checkbox *checkbox = (Checkbox*)[cell viewWithTag:2];
checkbox.isChecked = YES or NO based on item.inCart??
return cell;
}
虽然实际上不是在这里设置itemLabel.text和checkbox状态,但是你调用的方法configureCell:withItem:
可能/应该这样做吗?
关于你在导航之间没有保留状态的问题,可能只是你每次都将这个新的Checkbox
添加到单元格中,所以它出现在它的任何地方默认选中/未选中状态为。
此外,您可能需要在Checkbox
中尝试以下内容:
- (void)setIsChecked:(BOOL)isChecked {
if (isChecked_ = isChecked)
return;
[self setImage:[UIImage imageNamed:isChecked_?@"checked.png":@"unchecked.png"] forState:UIControlStateNormal];
self.selectedItem.inCart = [NSNumber numberWithBool:isChecked_];
}
- (IBAction) checkboxTapped {
if (self.isChecked == NO) {
self.isChecked = YES;
} else {
self.isChecked = NO;
}
}
编辑:根据@westsider的评论修改了self.selectedItem.inCart的分配
答案 1 :(得分:1)
您将需要某些代码,实际根据selectedItem.inCart的值设置复选框图像。我的猜测是,在ItemsVC中你需要实现-viewWillAppear:或-viewDidAppear:并遍历你的复选框,设置它们是合适的。
如果没有看到更多代码,我不能提出更多建议。