我有一个想要使用索引路径的数据环境。当我遍历数据时,我想递增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);
这感觉有点笨拙--有没有更好的方法呢?
发布于 2012-03-09 15:01:31
你可以试试这个--也许同样笨拙,但至少短一点:
NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
发布于 2012-08-09 02:25:13
这样就少了一行:
indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:actualIndexPath.section];
发布于 2015-10-30 08:19:51
在Swift上查看我的解决方案:
func incrementIndexPath(indexPath: NSIndexPath) -> NSIndexPath? {
var nextIndexPath: NSIndexPath?
let rowCount = numberOfRowsInSection(indexPath.section)
let nextRow = indexPath.row + 1
let currentSection = indexPath.section
if nextRow < rowCount {
nextIndexPath = NSIndexPath(forRow: nextRow, inSection: currentSection)
}
else {
let nextSection = currentSection + 1
if nextSection < numberOfSections {
nextIndexPath = NSIndexPath(forRow: 0, inSection: nextSection)
}
}
return nextIndexPath
}
https://stackoverflow.com/questions/9636086
复制相似问题