Dynamically appending a variable property name

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.

The buttons are cleverly named as a string: 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.

#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.

//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


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.

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

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:

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 ...
    }
}

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

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

链接地址: http://www.djcxy.com/p/72658.html

上一篇: iphone:如何混合像NSCalendar枚举

下一篇: 动态追加变量属性名称