6

インデックス パスを使用したいデータ状況があります。データをトラバースしているときに、NSIndexPath の最後のノードをインクリメントしたいと考えています。私がこれまでに持っているコードは次のとおりです。

int nbrIndex = [indexPath length];
NSUInteger *indexArray = (NSUInteger *)calloc(sizeof(NSUInteger),nbrIndex);
[indexPath getIndexes:indexArray];
indexArray[nbrIndex - 1]++;
[indexPath release];
indexPath = [[NSIndexPath alloc] initWithIndexes:indexArray length:nbrIndex];
free(indexArray);

これは少し不格好に感じます - もっと良い方法はありますか?

4

4 に答える 4

6

あなたはこれを試すことができます-おそらく同じように不格好ですが、少なくとも少し短いです:

NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
于 2012-03-09T15:01:30.960 に答える