Stanley
Stanley

Reputation: 4486

Is there a more convenient way to create a NSIndexPath object?

    NSIndexPath *index_path;
    NSUInteger   u_array[] = {0, 0, 0};                
    index_path = [NSIndexPath indexPathWithIndexes : u_array length : 3]; 

The above creates a NSIndexPath with length == 3. Is there any way to do the same with fewer statements ?

Upvotes: 1

Views: 2482

Answers (3)

nevan king
nevan king

Reputation: 113747

If you're talking about using it in a UITableView, there's the UIKit addition indexPathForRow:inSection:

+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section

Here's how to use it:

NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:3 inSection:4];

Upvotes: 3

jrturton
jrturton

Reputation: 119242

Only if you are using iOS (you've tagged this and , so not sure which) and you are only interested in a two-part index path (i.e. a table section and row) - if this is the case you can use [NSIndexPath indexPathForRow:inSection:]. This is described in the NSIndexPath UIKit additions section of the documentation.

Upvotes: 1

yuji
yuji

Reputation: 16725

This is probably the best you can do:

NSUInteger u_array[] = {0, 0, 0}; 
NSIndexPath *index_path = [NSIndexPath indexPathWithIndexes:u_array length:3];

Upvotes: 2

Related Questions