Removing a checkmark accessory that is placed by default in my TableView

Go To StackoverFlow.com

2

Brand new poster here on StackOverflow. I've been reading and learning here for some time, but I need to start asking a few questions and interacting, so any help with a few of my issues would be greatly appreciated.

I basically generate a list in a tableView of default sounds from a plist in cellForRowAtIndexPath. This generates me a list of say "x" sounds and one of those sounds has a default checkmark accessory placed on its cell. I have a plist file that stores the 'active' sound file's index value, this is how I put the original checkmark in the generated tableView. So, in the code below, as you can see, I load in the plist's value for the 'active' sounds index, however, when a user interacts with my tableView and they select a new sound, I need the default checkmark placed to be removed from view. Everything else is working fine. I can't seem to crack this simple problem, Im sure its a simple syntax issue, i just don't know how to get it done. Im sure the solution is a single line of code or two and its right under my nose. Thank you for help in advance.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSDictionary *dictionarySound = [NSDictionary dictionaryWithContentsOfFile:[self ActDataFilePath]];

NSNumber *defaultSoundIndex = [dictionarySound valueForKey:@"soundIndex"];

int theIntVal = [defaultSoundIndex integerValue]; 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//Everything below works just fine, checkmarks are removed and placed accordingly, sounds are played just fine.  I just need help above in removing the default checkmark

if(self.checkedPath)
{        
    UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:self.checkedPath];

    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}


cell.accessoryType = UITableViewCellAccessoryCheckmark;

self.checkedPath = indexPath;

....... other code below the plays my sounds on click and stores the newly active sound name and index value in the plist.

2012-04-04 22:01
by STRITZ


0

Alright boys and girls, here is the solution to my own question.

The checkmark is determined and placed in cellForRowAtIndexPath initially when the table is built based on my plist file. Here is the code for my cellForRowAtIndexPath that grabs my plist data, builds my table and sets the default selection with a checkmark.

NSDictionary *dicSound = [NSDictionary dictionaryWithContentsOfFile:[self ActDataFilePath]];

NSNumber *defaultSoundIndex = [dicSound valueForKey:@"soundIndex"];

//conversion of NSNumber to integer for comparison below to indexPath.row, both need to be int's
int theIntVal = [defaultSoundIndex integerValue];

static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CheckMarkCellIdentifier];

cell.textLabel.text = [[MyArray objectAtIndex:indexPath.row] objectForKey:@"name"];

if(indexPath.row == theIntVal)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}
else 
{
   cell.accessoryType = UITableViewCellAccessoryNone;

}

return cell;

When the user interacts with the tableView by clicking on another cell and didSelectRowAtIndexPath kicks in, this is how Im placing the new checkmark and how Im setting the old cell to have no accessory checkmark.

//This is a dictionary file that contains a number of values I need to grab
NSDictionary *dictionarySound = [NSDictionary dictionaryWithContentsOfFile:[self ActDataFilePath]];

NSNumber *defaultSoundIndex = [dictionarySound valueForKey:@"soundIndex"];

//conversion of NSNumber to integer for comparison below to indexPath.row, both need to be int's

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];               

if(self.checkedPath)
{        
    UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:self.checkedPath];

    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}

cell.accessoryType = UITableViewCellAccessoryCheckmark;

self.checkedPath = indexPath;

//THE FIX
[tableView reloadData];

The key here is the [tableView reloadData]; line as it reloads the table with the new options checked and removes the original checkmark. Obviously, Im reloading data which means that I have persistence built in in a plist file. I basically update the local dictionary's value for 'soundIndex' EVERY TIME a user clicks on a cell and then write that to the persistent plist file.

Hope this helps anyone else who is having this issue.

2012-04-05 07:41
by STRITZ


0

The usual way of dealing with this is to use didSelectRowAtIndexPath: to record the fact that the selection has changed and tell the table view to reload data, either all or just for the affected cells, then do the accessoryType configuration in cellAtRowForIndexPath:.

Other ways tend to fail if scrolling of the table is possible.

2012-04-04 22:13
by Phillip Mills
thanks for the quick reply on this and I somewhat understand what your saying, but isn't there a simple line of code that I can put at the beginning of didSelectRowAtIndexPath for example, it would be something like this... 'if(currentRowSelected == theIndexValueFromMyPlist) { do nothing } else { remove the checkmark from currentRowSelected and place it in the active cell} - STRITZ 2012-04-04 22:42
I'm not sure how well it will work but indexPath.row is going to be the new selection (assuming only one section for your table) so it would seem you want to compare that to theIntVal. But then selecting a third, different cell looks like it would get complicated. Perhaps setting checkedPath from your default when the view loads and then using that for the test... - Phillip Mills 2012-04-04 22:49
Not sure... hopefully someone will post some awesome line or two of code that makes me hate myself soon - STRITZ 2012-04-04 23:37
I solved the issue by reloading the table every time the user interacts with the table cells. I used [tableView reloadData]; Thanks for your contribution and attention to the matter. I'll post the full solution in a bit - STRITZ 2012-04-05 02:00


0

Try following code

cell.accessoryType = UITableViewCellAccessoryNone;
2012-04-05 04:39
by hayden
Ads