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

  • Home
  • SEARCH
  • 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 6247289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:50:33+00:00 2026-05-24T12:50:33+00:00

I have UITableViewCell with UILabel and UISwitch . By default all UISwitch is set

  • 0

I have UITableViewCell with UILabel and UISwitch. By default all UISwitch is set to off.

Once I will turn on the switch and then scroll through table the switch value is set to default again,i.e.Off

Below is the code which I have used:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell != nil) cell = nil;
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if (indexPath.row == 0) {
        UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
        lbl1.text = @"Some Text";
        [cell addSubview:lbl1];

        switch = [[UISwitch alloc] initWithFrame:CGRectMake(190, 10, 200, 30)];
        [switch setOn:NO];
        switch.tag = 1;
        [switch addTarget:self action:@selector(switchTapped:) forControlEvents:UIControlEventChanged];
        [cell addSubview:switch];
    }

}

// Below is my switchTapped method:

- (void) switchTapped: (id)sender {
    UISwitch *tapSwitch = (UISwitch *)sender;

    switch (tapSwitch.tag) {
        case 1:
            if (tapSwitch.on) {
              // do something
            }
            else {
                // do something
            }
            break;
        case 2:
            if (tapSwitch.on) {
                // do something
            }
            else {
                // do something
            }
            break;
}

Am I doing anything wrong over here?

Thank You.

  • 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-24T12:50:34+00:00Added an answer on May 24, 2026 at 12:50 pm

    You’re using really nasty code which regenerates the cell each time it’s needed:

    if(cell != nil) 
    {
       cell = nil; 
    }
    
    if (cell == nil) 
    { 
    ...
    }
    

    Do you bind the state of your switch to some retained object (e.g. Item model object, where single cell reflects an Item)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UILabel control (title) in a UITableViewCell (cell) where I set the
I have a custom UITableViewCell subclass. I have set the contentView of my cell
I have a UILabel thats inside a Custom UITableViewCell, I am trying to get
I have a custom UITableViewCell that contains a UISlider control and a UILabel which
I have some UILabel inside a UITableViewCell with opaque background color and white text.
I have a UITableViewCell. The textLabel.text is set to some value. To the cell's
I have a custom UITableViewCell with two labels ( UILabel ). The table cells
I have a custom UITableViewCell with a UILabel and a UITextView in it. I
I have a subclassed UITableViewCell called Comment, which has a UILabel containing some relevant
I am a newbie. I have a custom UITableViewCell with 3 items: a UILabel

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.