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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:48:57+00:00 2026-05-20T20:48:57+00:00

Using the code bellow in my table view data source method ( …cellForRowAtIndexPath: )

  • 0

Using the code bellow in my table view data source method (…cellForRowAtIndexPath:) I am creating UISwitch’s in only certain cells for a user settings view that goes into a popover view.

The switch’s are working however for some reason when I scroll up and down the view switch’s are being added and removed from random cells that I did not specify should have a UISwitch in them.

What am I doing wrong here?

This is some of my code:

if (section == 0) {

    if (row == 0) {
        cell.textLabel.text = @"Units";
    }
    if (row == 1) {
        cell.textLabel.text = @"Prefered System";
    }
    if (row == 2) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"Lock System";

        UISwitch *switch1 = [[UISwitch alloc] initWithFrame:switchFrame];
        if ([defaults boolForKey:kLockSystem]) {
            [switch1 setOn:YES animated:NO];
        }
        else {
            [switch1 setOn:NO animated:NO];
        }

        [switch1 addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
        switch1.tag = 1;
        cell.accessoryView = switch1;
        [switch1 release];
    }
    if (row == 3) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"Reset On Exit";

        UISwitch *switch2 = [[UISwitch alloc] initWithFrame:switchFrame];
        if ([defaults boolForKey:kResetOnExit]) {
            [switch2 setOn:YES animated:NO];
        }
        else {
            [switch2 setOn:NO animated:NO];
        }

        [switch2 addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
        switch2.tag = 2;
        cell.accessoryView = switch2;
        [switch2 release];
    }
}
  • 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-20T20:48:57+00:00Added an answer on May 20, 2026 at 8:48 pm

    I guess you’re using dequeueReusableCellWithIdentifier: to get a cell before executing that code. Your problem that the UISwitches are not removed from the cell before it gets returned by that method.

    With the code you are using, there should be a simple fix: Just set cell.accessoryView to nil for the rows that do not have a switch. Or set it nil unconditionally right after you get the cell, before you enter the posted bit of code.

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

Sidebar

Related Questions

In the code below I am using a recursive CTE(Common Table Expression) in SQL
I am using the jQuery clone() method to clone a table row, then using
Using the code below, I am returning an nvarchar field from MS SQL 2005
I am using the code below to save and restore the window position and
I am using the code snippet below, however it's not working quite as I
The code below is the code i am using. It works fine in thunderbird
If I am using a tree structure of nodes similar to the code below,
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I'm using this code, and I get the stack trace that is listed below.
I'm using code that I found on the CodeProject.com for a low-level keyboard hook

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.