1

I am storing selected indexPath inside a mutabledictionary `selectedRowsInSectionDictionary as follows.

For example in the following dictionary it shows, first section which is key. And in this section, first (1,0), second(1,1) and third(1,2) rows has been chosen and stored inside the dictionary.

enter image description here

I am trying to check whether or not these indexPath is stored inside the dictionary in the cellForRowAtIndexPath delegate method, but it always returns false. i am wondering what I am doing wrong?

if([selectedRowsInSectionDictionary objectForKey:@(indexPath.section)] == indexPath)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Shabbir Ahmad
  • 615
  • 7
  • 17
casillas
  • 16,351
  • 19
  • 115
  • 215
  • try isEqual: method in replace of == in your comparison conditional. Also make sure that the object returned from the dictionary is actually an NSIndexPath object. – Bamsworld Aug 14 '17 at 00:59
  • Possible duplicate of [How to compare two NSIndexPaths?](https://stackoverflow.com/questions/6379101/how-to-compare-two-nsindexpaths) – Shamas S Aug 14 '17 at 01:00
  • @ShamasS, actually my question is sligthly different, I have an array of indexPathes to check. – casillas Aug 14 '17 at 01:16

2 Answers2

3

[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)] is an NSMutableArray reference, not an indexPath, so the comparison will never be true.

I would suggest that you store NSMutableIndexSet in your dictionary, rather than an array. Your code would then be something like:

NSMutableIndexSet *selectedSet = selectedRowsInSectionDictionary[@(indexPath.section)];
if ([selectedSet containsIndex:indexPath.row] {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

To add/remove items to the dictionary using a 'toggle' you would use:

NSMutableIndexSet *selectedSet = selectedRowsInSectionDictionary[@(indexPath.section)];

if (selectedSet == nil) {
    selectedSet = [NSMutableIndexSet new];
    selectedRowsInSectionDictionary[@(indexPath.section)] = selectedSet;
}

if ([selectedSet containsIndex:indexPath.row]) {
    [selectedSet remove:indexPath.row];
} else {
    [selectedSet add:indexPath.row];
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • It shows the following error no visible interface for `NSMutableIndexSet` has `contains` method. – casillas Aug 14 '17 at 01:01
  • Sorry, there is a difference between the Swift and Objective-C method names. I have updated it – Paulw11 Aug 14 '17 at 01:10
  • do you have any idea for the related question https://stackoverflow.com/questions/48483622/reload-section-does-not-handle-properly – casillas Jan 28 '18 at 06:10
1

This is failing as the value of the dictionary is an array.

As far as I can tell

[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)]

Will return an array containing 3 elements (The NSIndexPaths). You should be able to modify your code to the following:

if([[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)] containsObject:indexPath]
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

I have confirmed this with the following testing code:

NSIndexPath *comparisonIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
NSDictionary *test = @{ @(1): @[[NSIndexPath indexPathForRow:1 inSection:0],
                                comparisonIndexPath,
                                [NSIndexPath indexPathForRow:3 inSection:0]]};
NSArray *indexPathArray = [test objectForKey:@(1)];
if ([indexPathArray containsObject:comparisonIndexPath]) {
    NSLog(@"Yeeehawww, let's do some stuff");
}
Lachlan
  • 312
  • 2
  • 15