2012-03-22 107 views
11

我的问题是:如何以编程方式设置NSIndexPath。以编程方式设置NSIndexPath

比如我添加方法:

- (void)setDefaultValue{ 
tempIndexPath = [NSIndexPath indexPathForRow:0 inSection:1]; 
} 

在的tableView委托-cellForRowAtIndexPath我想比较两个indexPath

如果([indexPath isEqual:方法tempIndexPath])...

但在这种情况下,我tempIndexPath =空(我认为 - 因为这是autorelease对象)

如何在这种情况下设置NSIndexPath?

谢谢,所有!

回答

16

添加保留

- (void)setDefaultValue{ 
    tempIndexPath = [[NSIndexPath indexPathForRow:0 inSection:1] retain]; 
} 

但你必须知道在未来的版本temIndexPath的。

编辑:我删除了错误的选项。

+0

创建一个属性只是为了保留一个对象我认为这是对问题的高估。除此之外,还有(外部)使成员可以从课外访问的效果,这可能是不受欢迎的,特别是考虑到我们正在谈论“临时”成员...... – Saphrosit 2012-03-22 13:33:24

+0

@Saphrosit,您是对的。回答编辑 – LuisEspinoza 2012-03-22 13:37:13

2

直接让retain你实例化后:

[tempIndexPath retain]; 

这会让你的对象的所有者,所以记得要release它,当你这样做。

1

你必须分配它并在之后释放它,定义它的方式是返回一个自动释放对象。