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 6355167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:42:37+00:00 2026-05-24T22:42:37+00:00

I have a tableview controller, with a label and a switch displayed in a

  • 0

I have a tableview controller, with a label and a switch displayed in a row. To add elements like this, I am using the approach below(as is recommended to avoid the data in the row getting mixed up).

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         UISwitch* testSwitch = [[UISwitch alloc] initWithFrame:
                                     CGRectMake(200, 
                                                0.5 * (cell.frame.size.height-30 ), 
                                                30, 30)];
        testSwitch.tag = 2;//If this is a dynamic value, it only works for 14 rows
        [cell.contentView addSubview:testSwitch];
        [testSwitch release];
}
    UISwitch *switch1 = (UISwitch*) [cell viewWithTag:2];
//cannot set a dynamic value here
        switch1.on = [<array value> boolValue];

I need the tag to be a dynamic value depending on the data on the current row so that I can add an action method when the value changes, and make according model related changes.
However, since the tag is set in the first section, if I make that dynamic, only 14 values(which are the number of rows displayed on the screen) are set.
What do I do so that I can store a dynamic value on all the rows of this switch for its target action method to retrieve?

Thanks..

–Edit—
Changed code to

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         UISwitch* testSwitch = [[UISwitch alloc] initWithFrame:
                                     CGRectMake(200, 
                                                0.5 * (cell.frame.size.height-30 ), 
                                                30, 30)];
        testSwitch.tag = indexPath.row;//If this is a dynamic value, it only works for 14 rows
        [cell.contentView addSubview:testSwitch];
        [testSwitch release];
}
    UISwitch *switch1 = (UISwitch*) [cell viewWithTag:indexPath.row];

    switch1.on = someboolValue; //CRASH as switch1 points to UItablviewcell and not UISwitch.
  • 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-05-24T22:42:39+00:00Added an answer on May 24, 2026 at 10:42 pm
    testSwitch.tag=[indexPath row]
    

    assuming you only have one section.


    ok, it now sounds like you are having a problem that
    [cell viewWithTag:indexPath.row]

    is locating the cell and not it’s subview so assuming you only have one uiswitch subview:

    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UISwitch class]]) {
            v.on = someboolValue;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a tableview controller, when i select a row its pushing another view
I have a controller that contains a tableView that spawns another controller modally to
I have a tableview with large images that fill the cells and the row
I have a tableview controller under a navigation controller. Some of my table cells
I have a TableView controller class that uses a fetched results controller to display
I have a problem getting the NSUserDefaults to pull out on a tab/nav/tableview controller
I have a view which contains a tableview, (i call this view the EquationView,
I have an UITabBar controller managing several controllers (using SDK 3.0). One of these
For example I have data model like this Entity: Store Entity: Inventory Entity:Product Attribute:
I have 2 view controller pages.Add Reminder page which contains save button as right

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.