比较NSIndexPath

Jas*_*son 12 uitableview nsindexpath ios

这将是一个非常愚蠢的事情,但我在我的cellForRowAtIndexPath中有这个代码:

if ([self.indexPathSelected compare:indexPath] == NSOrderedSame)
{
    NSLog(@" %d %d %d %d", self.indexPathSelected.row, self.indexPathSelected.section, indexPath.row, indexPath.section);
}
Run Code Online (Sandbox Code Playgroud)

这打印:

0 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 0.

我在这做错了什么?

Doc*_*Doc 35

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

if (self.indexPathSelected && [self.indexPathSelected compare:indexPath] == NSOrderedSame)
{
    NSLog(@" %d %d %d %d", self.indexPathSelected.row, self.indexPathSelected.section, indexPath.row, indexPath.section);
}
Run Code Online (Sandbox Code Playgroud)

根据NSIndexPath比较方法的文档:

参数

indexPath

要比较的索引路径.

该值不得为零.如果值为nil,则行为未定义.