@property (strong,nonatomic)NSMutableSet*selectedRows;
@property(nonatomic,retain) IBOutlet UITableView *tableView;
...
self.selectedRows = [NSMutableSet new];
...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedRows containsObject:indexPath]){
[self.selectedRows removeObject:indexPath];
}else{
[self.selectedRows removeAllObjects];//test 1 item only
[self.selectedRows addObject:indexPath];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"heightForRowAtIndexPath %@ \nSelected %@", indexPath,self.selectedRows);
for (NSIndexPath*item in self.selectedRows) {
if ([item compare:indexPath]==NSOrderedSame) {
NSLog(@"EXIST1");
}
if (item.section == indexPath.section && item.row == indexPath.row) {
NSLog(@"EXIST2");
}
}
if ([self.selectedRows containsObject:indexPath] == YES) {
NSLog(@"EXIST3");
}
return 44.0f;
}
After redrawing cells I expect filter selected cell.
The question is why I get only this in log: EXIST1 EXIST2
where is EXIST3?
simple test:
NSMutableSet*s =[NSMutableSet new];
NSIndexPath*p1 = [NSIndexPath indexPathForRow:1 inSection:1];
NSIndexPath*p2 = [NSIndexPath indexPathForRow:1 inSection:1];
NSComparisonResult r1= [p1 compare:p2];
NSLog(@"p1 compare p2 = %@", r1==NSOrderedSame?@"YES":@"NO" );
[s addObject:p1];
NSLog(@"contain p1 =%@", [s containsObject:p1]==YES?@"YES":@"NO" );
NSLog(@"contain p2 =%@", [s containsObject:p2]==YES?@"YES":@"NO" );
NSLog(@"s = %@",s);
result
2014-01-08 04:42:57.477 test[28342:70b] p1 compare p2 = YES
2014-01-08 04:42:57.478 test[28342:70b] contain p1 =YES
2014-01-08 04:42:57.478 test[28342:70b] contain p2 =YES
2014-01-08 04:42:57.479 test[28342:70b] s = {(
<NSIndexPath: 0x8b2e0d0> {length = 2, path = 1 - 1}
)}
small NSIndexPath category fix