首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较NSIndexPath

比较NSIndexPath
EN

Stack Overflow用户
提问于 2013-06-24 23:22:38
回答 2查看 11.5K关注 0票数 12

这将是非常愚蠢的事情,但我的cellForRowAtIndexPath中有以下代码:

代码语言:javascript
复制
if ([self.indexPathSelected compare:indexPath] == NSOrderedSame)
{
    NSLog(@" %d %d %d %d", self.indexPathSelected.row, self.indexPathSelected.section, indexPath.row, indexPath.section);
}

这将打印:

0 0 0

0 0 1 0

0 0 2 0

0 0 3 0

0 0 4 0

0 0 5 0

我期望它只打印0 0 0。

我在这里做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-25 01:43:06

由于您将indexPathSelected设置为nil,因此在进行比较之前,您需要确保它是非nil。

代码语言:javascript
复制
if (self.indexPathSelected && [self.indexPathSelected compare:indexPath] == NSOrderedSame)
{
    NSLog(@" %d %d %d %d", self.indexPathSelected.row, self.indexPathSelected.section, indexPath.row, indexPath.section);
}

根据NSIndexPath's compare method上的文档

参数

indexPath

要比较的索引路径。

该值不能为nil。如果值为nil,则行为未定义。

票数 35
EN

Stack Overflow用户

发布于 2014-11-26 15:39:56

有趣的是,你可以像比较指针一样比较对象。但只能在iPhone 5s或更高版本上使用。我今天发现了这个笑话)。所有设备上的iOS为8.1

将在iPhone 5s和更高版本上运行:(将比较段和行)

代码语言:javascript
复制
if (optionsPath != pathForOptionsCellOfSelected)
//if ([optionsPath compare:pathForOptionsCellOfSelected] != NSOrderedSame)
{
    //if user selects cell under already selected, we do not need to shift index
    if (optionsPath.row < selectedRowIndexPath.row)
    {
        pathForOptionsCellOfSelected = selectedRowIndexPath;
    }
    [_tableView insertRowsAtIndexPaths:@[pathForOptionsCellOfSelected] withRowAnimation:UITableViewRowAnimationTop];
    optionsPath = [pathForOptionsCellOfSelected copy];
}

无论如何,这不是一个好的方法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17279131

复制
相关文章

相似问题

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