Reputation: 11
I am currently developing an app for a local vet so his clients can order products from their iPhone. I've run into this strange bug, namely I can't deselect TableViewCells in a dynamic table.
When I press a cell in my tableview it becomes selected like it normally would. However when I select a cell for the second time it won't deselect the first one. Even when I press an already selected cell the won't deselect.
Does anyone have any tips on what I'm doing wrong or what I should do to solve this problem?
EDIT:
Here's my code for selecting a table:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
hasCellSelected = YES;
cell1 = [tableView cellForRowAtIndexPath:indexPath];
cell2 = [tableView dequeueReusableCellWithIdentifier:@"medicineCell"
forIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
long row = [indexPath row];
cell2.medicineLabel.text = [_medicineArray objectAtIndex:row];
if (prevIndexPath && ![prevIndexPath isEqual:indexPath])
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
prevIndexPath = indexPath;
}
Upvotes: 0
Views: 53
Reputation: 3271
If you want to deselect your current selected cell, You can use below code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
If you want to deselect your previous selected cell, just update the above method to
NSIndexPath * prevIndexPath;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (prevIndexPath && ![prevIndexPath isEqual:indexPath])
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
prevIndexPath = indexPath;
}
Upvotes: 0
Reputation: 24
You have multiple selection property set to YES.
Select your table , look at the right side of Xcode , the attributes Panel, there is a "allowMultipleSelection" property. Untick it.
Or place this in your viewDidLoad method
self.tableView.allowsMultipleSelection=NO;
Upvotes: 1