2011-06-23 12 views
35

Básicamente estoy almacenando un índice de una matriz en un NSInteger. Ahora lo necesito como un NSIndexpath, estoy luchando para ver y encontrar una forma de convertir mi NSInteger a NSIndexpath para poder reutilizarlo.Convertir NSInteger a NSIndexpath

+0

¿Qué necesita el 'NSIndexPath' para? ¿Lo necesita para un 'UITableView'? – albertamg

+0

Se usa cuando se llama a una declaración de actualización para sqlite. Creo que ahora me convertí a NSIndexPath pero necesito NSIndexPath.row y esto arroja un error. ¿Algunas ideas? – Dan

+0

No puede ** llamar ** a 'row' en una ruta de índice creada con' indexPathWithIndex: '. Para crear una ruta de índice puede llamar 'row' en ella, puede usar' indexPathForRow: inSection: '. – albertamg

Respuesta

86

Para un int index:

NSIndexPath *path = [NSIndexPath indexPathWithIndex:index]; 

Crea índice del elemento en el nodo 0 a punto como por the reference.

Para utilizar el indexPath en un UITableView, el método más apropiado es

NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section]; 
+0

Gracias. ¿Todavía puedo hacer path.row o me estoy cruzando mis eiders? – Dan

3

Utilice los siguientes métodos de la clase NSIndexPath.

+ (id)indexPathWithIndex:(NSUInteger)index; 
+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length; 
- (id)initWithIndex:(NSUInteger)index 

uso que a continuación y también No se olvide de releasemyIndexPath objeto después de usar.

NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndex:[myIntObj intValue]]; 
7

Si necesita un NSIndexPath para un UITableView, puede utilizar indexPathForRow:inSection: (reference). Las rutas de índice pasadas a la vista de tabla deben contener exactamente dos índices que especifiquen la sección y la fila. Una ruta de índice creada con indexPathWithIndex: solo contiene un índice y no funcionará con una vista de tabla.

2

Swift 3:

let indexPath = IndexPath(row: 0, section: 0)