动态附加变量属性名称

时间:2022-02-11 01:35:35

I have a custom UITableViewCell with multiple buttons. I would like to remember whether the buttons are in the selected or unselected state and store that in a property of a custom core data model class. There are multiple custom UITableViewCells, and each has a different number of buttons.

我有一个带有多个按钮的自定义UITableViewCell。我想记住按钮是处于选定状态还是未选定状态,并将其存储在自定义核心数据模型类的属性中。有多个自定义UITableViewCell,每个都有不同数量的按钮。

The buttons are cleverly named as a string: 1,2,3...

这些按钮巧妙地命名为字符串:1,2,3 ......

To explain the project: imagine a teacher that wanted to keep track of the number of chapters read by a student for a list of books. The goal is to track the total number of chapters read for each student. Each book is a UITableViewCell. Each book has a unique number of chapters. The teacher (or student) selects a button when each chapter is read. The chapter read would be saved as a property so that it could be presented as such the next time the UITableViewCell displayed.

为了解释这个项目:想象一位老师想要跟踪学生阅读的书籍列表的章节数量。目标是跟踪每个学生阅读的章节总数。每本书都是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.


}

So, my question is, how best to store the button state into the student property for the selected chapter? I wish I could append the chapter number to the 'student.ch[append chapter number here]', but I don't think that is possible.

所以,我的问题是,如何最好地将按钮状态存储到所选章节的学生属性中?我希望我可以将章节编号附加到'student.ch [在这里添加章节编号],但我不认为这是可能的。

//eg.
student.ch1 = [NSNumber numberWithBool:YES];//but replace '1' with the value in the int variable 'chapter'

Thank you in advance. I think I'm barking up the wrong tree.

先谢谢你。我想我正在咆哮错误的树。

Kurt

库尔特

2 个解决方案

#1


1  

Since the number of buttons is small (you indicated 28 in the comment), you can use powers of two as tags on your buttons, and use an integer bitmask to store the state of all 28 buttons in a single integer field.

由于按钮数量较少(您在注释中指示了28个),因此您可以在按钮上使用2的幂作为标记,并使用整数位掩码将所有28个按钮的状态存储在单个整数字段中。

Consider this example with four buttons (you can expand it to 32 without much changes). Tag your buttons as follows:

考虑这个带有四个按钮的示例(您可以将其扩展为32而无需太多更改)。标记您的按钮如下:

button1.tag = 0x01; // Binary 0001
button2.tag = 0x02; // Binary 0010
button3.tag = 0x04; // Binary 0100
button4.tag = 0x08; // Binary 1000

When a button is selected, bitwise-OR its tag with the current state:

选择按钮时,将其标记与当前状态按位或运算:

NSUInteger currentState = 0;
...
currentState |= button.tag;

When a button is un-selected, bitwise-AND its tag's inverse with the current state:

当一个按钮未被选中时,按位-AND其标签与当前状态相反:

currentState &= ~button.tag;

To toggle the state, you can XOR the tag with the current state:

要切换状态,可以使用当前状态对标记进行异或:

currentState ^= button.tag;

When you need to re-apply the selected/not selected state to your buttons, you can do it in a loop like this:

当您需要将所选/未选中状态重新应用于按钮时,可以像这样循环:

for (int i = 0 ; i != 28 ; i++) {
    NSUInteger tag = 1<<i;
    if (storedState & tag) {
        UIButton *btn = [myView viewWithTag:tag];
        // ... make the button selected ...
    }
}

#2


0  

If I got your question right, it is easy to answer: Have a look at Key-Value-Coding - that should help you!

如果我的问题是正确的,那么很容易回答:看看Key-Value-Coding - 这对你有帮助!

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html

#1


1  

Since the number of buttons is small (you indicated 28 in the comment), you can use powers of two as tags on your buttons, and use an integer bitmask to store the state of all 28 buttons in a single integer field.

由于按钮数量较少(您在注释中指示了28个),因此您可以在按钮上使用2的幂作为标记,并使用整数位掩码将所有28个按钮的状态存储在单个整数字段中。

Consider this example with four buttons (you can expand it to 32 without much changes). Tag your buttons as follows:

考虑这个带有四个按钮的示例(您可以将其扩展为32而无需太多更改)。标记您的按钮如下:

button1.tag = 0x01; // Binary 0001
button2.tag = 0x02; // Binary 0010
button3.tag = 0x04; // Binary 0100
button4.tag = 0x08; // Binary 1000

When a button is selected, bitwise-OR its tag with the current state:

选择按钮时,将其标记与当前状态按位或运算:

NSUInteger currentState = 0;
...
currentState |= button.tag;

When a button is un-selected, bitwise-AND its tag's inverse with the current state:

当一个按钮未被选中时,按位-AND其标签与当前状态相反:

currentState &= ~button.tag;

To toggle the state, you can XOR the tag with the current state:

要切换状态,可以使用当前状态对标记进行异或:

currentState ^= button.tag;

When you need to re-apply the selected/not selected state to your buttons, you can do it in a loop like this:

当您需要将所选/未选中状态重新应用于按钮时,可以像这样循环:

for (int i = 0 ; i != 28 ; i++) {
    NSUInteger tag = 1<<i;
    if (storedState & tag) {
        UIButton *btn = [myView viewWithTag:tag];
        // ... make the button selected ...
    }
}

#2


0  

If I got your question right, it is easy to answer: Have a look at Key-Value-Coding - that should help you!

如果我的问题是正确的,那么很容易回答:看看Key-Value-Coding - 这对你有帮助!

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html