这将是非常愚蠢的事情,但我的cellForRowAtIndexPath中有以下代码:
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。
我在这里做错了什么?
发布于 2013-06-25 01:43:06
由于您将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);
}根据NSIndexPath's compare method上的文档
参数
indexPath
要比较的索引路径。
该值不能为nil。如果值为nil,则行为未定义。
发布于 2014-11-26 15:39:56
有趣的是,你可以像比较指针一样比较对象。但只能在iPhone 5s或更高版本上使用。我今天发现了这个笑话)。所有设备上的iOS为8.1
将在iPhone 5s和更高版本上运行:(将比较段和行)
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];
}无论如何,这不是一个好的方法。
https://stackoverflow.com/questions/17279131
复制相似问题