Pizzaiola Gorgonzola
Pizzaiola Gorgonzola

Reputation: 2899

how to set a NSIndexPath after it has been created

i created a NSIndexPath using:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

now i want to index thru a UITableView by modifying this path, i can't find any API calls that set values in a NSIndexPath

seems like a huge waste to allocate a new NSIndexPath every time, especialy as i'm actually using it to make a call that doesn't allocate any memory (an addition to the protocol)

Upvotes: 2

Views: 995

Answers (2)

rob mayoff
rob mayoff

Reputation: 385930

NSIndexPath is immutable. Just create a new one each time. Then, if you have a performance problem, use Instruments to figure out whether creating index paths is part of the problem.

Only after you have verified that creating index paths is a problem should you consider creating a subclass of NSIndexPath that has its own storage for the indexes and provides mutation methods. (UIKit does something like this internally with UIMutableIndexPath, but that class is not public.)

Upvotes: 2

user529758
user529758

Reputation:

I can't find any API calls that set values in an NSIndexPath.

Because there aren't - NSIndexPath is immutable.

seems like a huge waste to allocate a new NSIndexPath every time

It is. Sorry for that. Wrap it into an @autoreleasepool { } if you care about this.

Upvotes: 2

Related Questions