Reputation: 7343
Sorry but I've been stuck with this tricky problem. I have a tableView and once the user clicks it, a checkmark will appear and it is highlighted. However, when the user clicks it again, only the checkmark is gone and the highlight remains. On the third click, the checkmark is gone but I get the highlight. On the fourth click, the tableViewCell is finally cleared. I suspect this has something to do with my didSelectRowAtIndexPath
method.
Here is my cellForRowAtIndexPath
method.
What I basically do here is that I set the next labels and the checkmark if needed.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ItemCellIdentifier = @"CellIdentifier";
SearchResultTableViewCell *tableCell =(SearchResultTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier];
//set text labels here
[tableCell.price setText:[NSString stringWithString:[priceList objectAtIndex:indexPath.row]]];
[tableCell.itemName setText:[NSString stringWithString:[itemNameList objectAtIndex:indexPath.row]]];
//set check mark and highlights.
if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
tableCell.accessoryType = UITableViewCellAccessoryNone;
}
return tableCell;
}
Here is my didSelectRowAtIndexPath
method. In this method, I toggle the checkMark value by switching it to 0 if it's 1 and vice versa, so when the [self.searchResults reloadData];
gets called, it knows which rows needs the checkmark and which ones do not. I also set the selected cell as selected or not because I save the index path for the selected rows so when I reload the table, I know which ones were selected and I can highlight it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
[checkList replaceObjectAtIndex:indexPath.row withObject:@"0"];
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO];
}
else{
[checkList replaceObjectAtIndex:indexPath.row withObject:@"1"];
[[tableView cellForRowAtIndexPath:indexPath] setSelected:YES];
}
NSArray *indexPaths = [self.searchResults indexPathsForSelectedRows];
//searchResults is my tableView
[self.searchResults reloadData];
for (NSIndexPath *indexPath in indexPaths) {
[self.searchResults selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
I have a feeling I'm doing the [[tableView cellForRowAtIndexPath:indexPath] setSelected:NO];
wrong. How do I properly access the tableViewCell from the tableView object and how do I properly set it as selected or not?
Thanks in advance
Upvotes: 0
Views: 1138
Reputation: 1245
I think the
[self.searchResults reloadData];
maybe causing the table to be reloaded and then independently figure out if a cell is selected or not. I use this command all the time particularly when there are changes to the underlying array or data, and it maybe important to what you are doing when the row is selected. In which case you amy want to do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *indexPaths = [self.searchResults indexPathsForSelectedRows];
//searchResults is my tableView
[self.searchResults reloadData];
for (NSIndexPath *indexPath in indexPaths) {
[self.searchResults selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
[checkList replaceObjectAtIndex:indexPath.row withObject:@"0"];
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO];
}
else{
[checkList replaceObjectAtIndex:indexPath.row withObject:@"1"];
[[tableView cellForRowAtIndexPath:indexPath] setSelected:YES];
}
}
Upvotes: 0
Reputation: 11598
In your -didSelectRowAtIndexPath:
, make sure to call [tableView deselectRowAtIndexPath:indexPath animated:YES]
.
Change value passed to animated
as desired.
Just noticed, I wouldn't set selected
manually in -didSelectRowAtIndexPath:
. I'd set the cell selected
or not only in the configuration of the cell. Take out your call to setSelected:
and see if it works.
Upvotes: 1