Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:00:59+00:00 2026-06-10T02:00:59+00:00

I have a custom UITableViewCell with multiple buttons. I would like to remember whether

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:01:00+00:00Added an answer on June 10, 2026 at 2:01 am

    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 ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom UITableViewCell that seems to appear multiple times on my table
I have a custom uitableviewcell with several labels and I would like for some
I have a custom (subclass of) UITableViewCell with multiple UITextFields in it. I would
I have a custom UITableViewCell that has 2 buttons and a label on it.
I have a custom UITableViewCell that I use like this: AppTableCell *cell = [tableView
I have a custom UITableViewCell that I defined in a storyboard. I added a
I have a custom UITableViewCell class, and within that I've overridden the drawRect method
I have a property with name bgImage in my custom UITableViewCell class (MyTableCell). Its
I have a custom UITableViewCell that consists of a UIImageView and a UILabel. The
I have a custom UITableView that allows the user to select multiple images. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.