Explanation needed (UITableView, NSMutableSet)

249 views Asked by At
@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}
)}
2

There are 2 answers

0
Andrii Tishchenko On

small NSIndexPath category fix

@interface NSIndexPath (Extended)
-(NSUInteger)hash;
@end


@implementation NSIndexPath (Extended)
-(NSUInteger)hash{
    char str[11];
    int row = self.row;
    int section = self.section;
    sprintf(str, "%d%d", section,row);

    unsigned int val = 0;
    char *p;
    int i;
    p = str;
    for(i = 0; p[ i ]; i++){
        if (i ==0) {
            val = (unsigned char)p[i] << CHAR_BIT;
        }
        else
            val |= (unsigned char)p[i];
    }
    return val;
}
@end
0
Tom Jones On

I had the same problem, I solved it by filtering the set. I'm not sure which is more efficient, but this was performant enough for my purposes. code is from a call to heightForRowAtIndexPath

NSSet * set = [self.hiddenPaths filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"section == %u AND row == %u",indexPath.section, indexPath.row]];

if (![set count])
{
  return 0.0f;//cell at indexPath is hidden
}