iOS查询多维数组并返回索引路径



我有一个NSMutableArray,它持有其他NSMutableArray(我将称它们为子数组)。这些子数组每个都包含一个惟一的变量id。我想返回子数组的索引路径它包含一个特定的id。例如,如果其中一个子数组的id为25,我如何找到该子数组的索引路径(而不是子数组中id的索引路径)?

提前谢谢你。如果你有任何问题,请告诉我。

艾凡,

欢呼。

基本上必须手动搜索所有子数组。例如:

NSUInteger outerIndex = [outerArray indexOfObjectPassingTest:^(id subarray, NSUInteger outerIdx, BOOL *stopOuter) {
    NSUInteger innerIndex = [subarray indexOfObjectPassingTest:^(id obj, NSUInteger innerIdx, BOOL *stopInner) {
        return [obj isEqual:searchTerm];
    }];
    return innerIndex != NSNotFound;
}];
NSUInteger indexes[] = {outerIndex, innerIndex};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:2];

从NSArray类引用中,我找到了下面的方法。

indexOfObject:
Returns the lowest index whose corresponding array value is equal to a given object.
- (NSUInteger)indexOfObject:(id)anObject
Parameters
anObject
An object.
Return Value
The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.
Discussion
Objects are considered equal if isEqual: returns YES

如果你有一个数组,它包含你的索引id,然后将它与你的主数组一起传递给这个方法,你将得到数组的相应索引,它包含你的id。

.

最新更新