Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 比较nsindepath_Ios_Uitableview_Nsindexpath - Fatal编程技术网

Ios 比较nsindepath

Ios 比较nsindepath,ios,uitableview,nsindexpath,Ios,Uitableview,Nsindexpath,这将是非常愚蠢的事情,但我的CellForRowatineXpath中有以下代码: 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

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

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

01 0

020

030

040

050

我希望它只打印0


我在这里做错了什么?

因为您将indexPathSelected设置为nil,所以在进行比较之前,您需要确保它不是nil

if (self.indexPathSelected && [self.indexPathSelected compare:indexPath] == NSOrderedSame)
{
    NSLog(@" %d %d %d %d", self.indexPathSelected.row, self.indexPathSelected.section, indexPath.row, indexPath.section);
}
根据以下文件:

参数

索引

要比较的索引路径

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


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

将适用于iPhone 5s及更高版本:(将比较节和行)

if(optionpath!=pathForOptionsCellOfSelected)
//if([OptionPath compare:pathForOptionsCellOfSelected]!=SensorDeredName)
{
//若用户选择“已选择”下的单元格,则不需要移动索引
if(optionsPath.row

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

是否将
indexPathSelected
设置为nil?是。将其更改为self.indexPathSelected=[NSIndexPath indexPathForRow:-1节:-1];修好了。两个后续问题,“零”为什么不起作用?第二个是将其设置为-1,-1,这是正确的修复方法?向nil发送消息将始终导致nil=>0。只需检查是否与nil进行比较。改用
[indexPath compare:self.indexPathSelected]
,则无需检查self.indexPathSelected是否为nil。
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];
}