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