Question

How to check if a collection of NSIndexPath objects have item in a specific section? I don't care about the row, and want to know yes/no if it has items of a particular section.

As is, I have written out statically based on the number of rows in the program, so for instance, a section 0 with 3 rows looks like:

if ([array containsObject:[NSIndexPath indexPathForItem:0 inSection:0]] || [array containsObject:[NSIndexPath indexPathForItem:1 inSection:0]] || [array containsObject:[NSIndexPath indexPathForItem:2 inSection:0]]) 
Was it helpful?

Solution

Assuming array contains an array of NSIndexPath objects:

NSInteger someSection = ... // the section to check for
BOOL exists = NO;
for (NSIndexPath *path in array) {
    if (path.section == someSection) {
        exists = YES;
        break;
    }
}

if (exists) {
    // Yes, at least one of the index paths was for the desired section
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top