ios 以编程方式设置 NSIndexPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9823159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:29:31  来源:igfitidea点击:

Set NSIndexPath programmatically

objective-ciosnsindexpath

提问by Matrosov Alexander

My question is: How to set NSIndexPath programmatically.

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

For example I add method:

例如我添加方法:

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

In tableView delegate -cellForRowAtIndexPathI want to compare two indexPath

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

if([indexPath isEqual:tempIndexPath]) ...

if([indexPath isEqual:tempIndexPath]) ...

But in this case my tempIndexPath = null (i think - because this is autorelease object)

但在这种情况下,我的 tempIndexPath = null (我认为 - 因为这是自动释放对象)

How to set NSIndexPath in this case?

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

Thanks, All!

谢谢大家!

回答by LuisEspinoza

Add retain

添加保留

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

But you have to be aware of release temIndexPath in the future.

但是您必须注意将来发布 temIndexPath。

EDIT:I deleted bad option.

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

回答by Manlio

Simply call retainafter you instantiated it:

retain实例化后只需调用:

[tempIndexPath retain];

This will make you the owner of the object, so remember to releaseit when you have done.

这将使您成为对象的所有者,因此请在release完成后记住它。

回答by Alexander

You have to allocate it and release it afterwards, defining it the way you did is returning an autoreleased object.

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