首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何递增NSIndexPath

如何递增NSIndexPath
EN

Stack Overflow用户
提问于 2012-03-09 22:53:33
回答 4查看 7.3K关注 0票数 6

我有一个想要使用索引路径的数据环境。当我遍历数据时,我想递增NSIndexPath的最后一个节点。到目前为止,我拥有的代码是:

代码语言:javascript
运行
复制
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);

这感觉有点笨拙--有没有更好的方法呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-09 23:01:31

你可以试试这个--也许同样笨拙,但至少短一点:

代码语言:javascript
运行
复制
NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
票数 6
EN

Stack Overflow用户

发布于 2012-08-09 10:25:13

这样就少了一行:

indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:actualIndexPath.section];

票数 5
EN

Stack Overflow用户

发布于 2015-10-30 16:19:51

在Swift上查看我的解决方案:

代码语言:javascript
运行
复制
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
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9636086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档