Note that there are some explanatory texts on larger screens.

plurals
  1. POToggle Boolean on UITableView Selected Row
    primarykey
    data
    text
    <p>I'm trying to toggle a Boolean value when a table row is selected. This code was working when my table rows contained NSObjects (in a different class)...I simplified my code so that the rows only contain strings and not objects (cut out the class), but now the boolean won't toggle because my instance variable is a string. Here's the original working code:</p> <p>Declared in XYZObject:</p> <pre><code>@property BOOL checked; </code></pre> <p>Table view didSelectRowAtIndexPath:</p> <pre><code>XYZObject *tappedItem = [myArray objectAtIndex:indexPath.row]; tappedItem.checked = !tappedItem.checked; NSString *addedItem = cell.textLabel.text; if(!tappedItem.checked) { [newArray removeObject:addedItem]; } else { [newArray addObject:addedItem]; } </code></pre> <p>When I change XYZObject to NSString and move the Bool property to the table view, I get the "Property 'checked' not found on object of type 'NSString *'. Changing NSString to NSObject does the same thing. If I change NSString to XYZMyTableViewController, it will now compile but then breaks at runtime due to "unrecognized selector sent to instance".</p> <p>So my question would be, can I toggle the 'checked' Bool value on an array of strings?</p> <p><strong>UPDATE/SOLUTION</strong></p> <p>I was able to solve the problem by eliminating the Bool entirely and just using:</p> <pre><code>if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; [newArray addObject:addedItem]; } else { cell.accessoryType = UITableViewCellAccessoryNone; [newArray removeObject:addedItem]; } </code></pre> <p>I'm not particularly happy as I had some other methods using 'checked', but I should be able to just rewrite them in the same way. I'd still like to know why I wasn't able to toggle a bool value on a string that's in my table cell.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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