-2

I m trying to store selected indexpaths for some purpose. Below code I m executing in cellForRowAtIndexpath:

 if ([_arrOfIndexPaths containsObject:indexPath]) {
        [filterCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [filterCell.textLabel setFont:[UIFont boldSystemFontOfSize:filterCell.textLabel.font.pointSize]];
    }

When I m trying to print the description following line gets printed.

<__NSArrayM 0x91567d0>(
<NSIndexPath: 0x8e8b5b0> {length = 2, path = 0 - 0},
<NSIndexPath: 0x91559a0> {length = 2, path = 2 - 0}
)
Printing description of indexPath:
<NSIndexPath: 0x91a8460> {length = 2, path = 0 - 0}

If you see the instance description 0x91a8460 which is not available in the array. But still condition succeeded. Could some one explain me how this works. Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rajesh
  • 10,318
  • 16
  • 44
  • 64
  • 2
    Read the docs for the `containsObject:` method. The objects are compared with a call to `isEqual:`. – rmaddy Jul 08 '14 at 05:27

2 Answers2

1

Instance 0x91a8460 is not present in the array, but path 0-0 is. Comparison for containsObject: is performed by isEqual, not by object identity. If you want an object-identity comparison, use indexOfObjectIdenticalTo:.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

In my opinion, it is not ideal to store indexPath(s) in an array then use it to update table cell. It maybe better to use array of dictionary where you have all your table data there, then you can add a key like isCheck and set it TRUE or FALSE after user action.

Or

You can still have your array but instead of storing the indexpath better make a string combination like (@"%d%d",indexpath.row, indexpath.section) and use it to string compare.

Hope that helps.

Arnlee Vizcayno
  • 2,405
  • 3
  • 23
  • 31