以编程方式设置NSIndexPath

13

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

例如,我添加了一个方法:

- (void)setDefaultValue{
 tempIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
}
在tableView代理-cellForRowAtIndexPath中,我想比较两个indexPath。if([indexPath isEqual:tempIndexPath]) ...但是在这种情况下,我的tempIndexPath = null(我认为是因为这是自动释放对象)。如何在这种情况下设置NSIndexPath?谢谢大家!
3个回答

22

添加保留

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

但是您必须注意在未来释放temIndexPath。

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


创建一个属性只是为了保留一个对象,我认为这是对问题的高估。此外,这会导致成员可以从类外部访问,这可能是不希望的,特别是考虑到我们正在谈论一个“temp”成员... - Manlio

2

在实例化后简单地调用retain

[tempIndexPath retain];

这会使你成为对象的所有者,因此请记得在完成后释放它。


1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接