Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>TableViewCells get destroyed and created where necessary when the table view gets scrolled. </p> <p>When a cell gets destroyed as it goes out of the visible screen area, your switch control being a subview of the cell also gets destroyed.</p> <p>So when you scroll back to one of your previously set switches, what you're actually seeing is another instance of a UITableViewCell with a switch view added to it, and so it looks like the switch value changed back to NO.</p> <p>What you need is a third thing that remembers what the value of each switch should be in each row. If you're display a table of Core Data entity information, then perhaps you can define a property for your entity like "active". In that case, every time you change a switch value, you set your core data entity "active" property to the switch value e.g.:</p> <pre><code>-(UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... if(cell == nil) { // set switch control tag number (don't use 0) } // get switch control by tag number // pseudocode Engine *myEngine = [arrEngine objectAtIndex:indexPath.row]; mySwitchControl.active = myEngine.active; ... } // when you change switch value, remember to update your core data entity value -(void)updateSwitchValue:(BOOL) newValue { // update value } </code></pre> <p>Otherwise, you can simply use a NSMutableArray of bool values to identify which row each switch should be YES or NO:</p> <pre><code>// my header file (.h file) @interface { NSMutableArray *arrSwitchValues; } // my implementation file (.m file) -(UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... if(cell == nil) { // set switch control tag number (don't use 0) } // get switch control by tag number // pseudocode BOOL switchVal = [arrSwitchValues objectAtIndex:indexPath.row]; mySwitchControl.active = switchval; ... } // when you change switch value, remember to update your array -(void)updateSwitchValue:(BOOL) newValue { // update value } </code></pre> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload