我有一个带有多个按钮的自定义UITableViewCell。我想记住按钮是处于选定状态还是未选定状态,并将其存储在自定义核心数据模型类的属性中。有多个自定义UITableViewCell,每个都有不同数量的按钮。
按钮巧妙地命名为字符串:1,2,3 ......
解释项目:想象一位老师想要跟踪学生阅读的书籍列表的章节数量。目标是跟踪每个学生阅读的章节总数。每本书都是UITableViewCell。每本书都有一个独特的章节。教师(或学生)在阅读每章时选择一个按钮。读取的章节将保存为属性,以便下次显示UITableViewCell时可以显示它。
#import "Student.h"
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
chaptersInBook = 16;
self.dickensArray = [NSMutableArray array];
// Book title
UILabel *bookLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 30)];
bookLabel.text = @"David Copperfield";
for (NSInteger index = 0; index < chaptersInBook; index++) // for loop runs 16 times
{
// Need to make correct number of buttons based on the chapters in each book
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = index;
//buttons in rows of seven
button.frame = CGRectMake(40*(index%7) + 20,40 * (index/7) + 40, 30, 30);
[button setTitle:[NSString stringWithFormat:@"%d", index+1] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",index+1]] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"%d", index+1] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(toggleOnOff:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:button];
[self.contentView addSubview:bookLabel];
}
}
return self;
}
-(IBAction)toggleOnOff:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected; // In storyboard the default image and selected image are set
}
-(IBAction)buttonPressed:(id)sender {
UIButton* button = (UIButton *)sender;
if (button.selected) {
int chapter = button.tag + 1;
NSString *nameOfButton = [NSString stringWithFormat:@"%d",chapter];
NSString *buttonIsSelected = @"YES";
//Now I want to set student.ch1 to yes but I want to set the '1' to 'chapter'
//Not sure how to do this: append the name of a property with a variable.
}
所以,我的问题是,如何最好地将按钮状态存储到所选章节的学生属性中?我希望我可以将章节编号附加到'student.ch [在这里添加章节编号],但我认为这不可能。
//eg.
student.ch1 = [NSNumber numberWithBool:YES];//but replace '1' with the value in the int variable 'chapter'
提前谢谢你。我想我正在咆哮错误的树。
库尔特
答案 0 :(得分:1)
由于按钮数量较少(您在注释中指示了28),您可以在按钮上使用2的幂作为标记,并使用整数位掩码将所有28个按钮的状态存储在单个整数字段中。
使用四个按钮考虑此示例(您可以将其扩展为32而无需太多更改)。按如下方式标记按钮:
button1.tag = 0x01; // Binary 0001
button2.tag = 0x02; // Binary 0010
button3.tag = 0x04; // Binary 0100
button4.tag = 0x08; // Binary 1000
选择按钮时,bitwise-OR
其标签为当前状态:
NSUInteger currentState = 0;
...
currentState |= button.tag;
取消选中某个按钮时,bitwise-AND
其标记与当前状态相反:
currentState &= ~button.tag;
要切换状态,您可以使用当前状态XOR
标记:
currentState ^= button.tag;
当您需要将所选/未选择状态重新应用于按钮时,您可以像这样循环执行此操作:
for (int i = 0 ; i != 28 ; i++) {
NSUInteger tag = 1<<i;
if (storedState & tag) {
UIButton *btn = [myView viewWithTag:tag];
// ... make the button selected ...
}
}
答案 1 :(得分:0)
如果我的问题是正确的,那么很容易回答:看一下Key-Value-Coding - 它应该对你有帮助!