Question

I store touched Items from my UICollectionView into a NSMutableArray, then reloadData.

My question is, how can I check if IndexPath.row is equal to any of my NSMutableArray objects?

I can do something like:

if(indexPath.row == [myMutableArray objectAtIndex:0])

But in this case, it will check only for first object from my NSMutableArray, I want to check all available objects if any of them are == to indexPath.row

I'm a beginner, and trying to figure out.

Thanks.

Was it helpful?

Solution 4

I'm assuming you are correctly storing values in myMutableArray as NSNumbers.

If you need to know the position in myMutableArray, use indexOfObject::

NSUInteger position = [myMutableArray indexOfObject:@(indexPath.row)];
if (position != NSNotFound) {
    // handle object appearing in the array
}

If you just need to know whether the object appears in the array, use containsObject::

if ([myMutableArray containsObject:@(indexPath.row)]) {
    // handle object appearing in the array
}

OTHER TIPS

for(int i=0 ; i < [myMutableArray count] ; i++) {

   if(indexPath.row == [[myMutableArray objectAtIndex:i]integerValue])

    {
          NSlog(@"equal");
          // your actions
    }
   else
   {
           NSlog(@"not equal");
          // your actions
   }

}

  if (indexPath.row == [[myMutableArray objectAtIndex:indexPath.row] integerValue])
            {
            }

   // Change your if Condition with that Code it will helps you. 

Try this:

NSNumber *num=[NSNumber numberWithInteger:indexPath.row];
NSInteger anIndex=[myMutableArray indexOfObject:num];
if(NSNotFound == anIndex) {
    NSLog(@"not found");
}else {
    // anIndex contains the index. Do whatever you want.
}

Hope this helps .. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top