Thursday, 19 September 2013

How to I make check marks appear and disappear when I click multiple rows?

How to I make check marks appear and disappear when I click multiple rows?

I want a table which, when you click different rows, they gain or lose a
checkmark to show they are either selected or not selected.
Currently my tableview will let me select and deselect different rows. But
a checkmark will only appear once I have clicked one and then another. The
same happens when I deselect, I click a row with a checkmark then click
another row and the checkmark disappears.
Here is my code currently:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * exerciseChoiceSimpleTableIdentifier =
@"ExerciseChoiceSimpleTableIdentifier";
UITableViewCell * exerciseChoiceCell = [tableView
dequeueReusableCellWithIdentifier:exerciseChoiceSimpleTableIdentifier];
if (exerciseChoiceCell == nil) {
exerciseChoiceCell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:exerciseChoiceSimpleTableIdentifier];
}
//Here we get each exercise we want to display
BExercise * exercise = [[_data getExerciseCompleteList]
objectAtIndex:indexPath.row];
//Name the cell after the exercises we want to display
exerciseChoiceCell.textLabel.text = exercise.name;
return exerciseChoiceCell;
}
-(void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType ==
UITableViewCellAccessoryNone)
{
// Uncheck the row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView cellForRowAtIndexPath:indexPath].accessoryType =
UITableViewCellAccessoryCheckmark;
}
else
{
// Check the row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView cellForRowAtIndexPath:indexPath].accessoryType =
UITableViewCellAccessoryNone;
}
}
I think the problem is to do with it being selected rather than touch up
inside and deselecting when the finger is released from the button.
I am expecting the answer to be quite straight forward but all the similar
questions on stackflow haven't dealt with the checkmarks delayed
appearance.
It would be great to understand the root of this problem and how to fix it.

No comments:

Post a Comment