13

My question is: How to set NSIndexPath programmatically.

For example I add method:

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

In tableView delegate -cellForRowAtIndexPath I want to compare two indexPath

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

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

How to set NSIndexPath in this case?

Thanks, All!

Manlio
  • 10,768
  • 9
  • 50
  • 79
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

3 Answers3

22

Add retain

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

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

EDIT:I deleted bad option.

LuisEspinoza
  • 8,508
  • 6
  • 35
  • 57
  • Creating a property just to retain an object I think it's an overestimation of the problem. Besides this has the (side) effect of making the member accessible from outside the class, which may be undesirable, especially considering that we are talking about a "temp" member... – Manlio Mar 22 '12 at 13:33
2

Simply call retain after you instantiated it:

[tempIndexPath retain];

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

Manlio
  • 10,768
  • 9
  • 50
  • 79
1

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

Alexander
  • 8,117
  • 1
  • 35
  • 46