parse.com PFQueryTableViewController devuelve el mismo objeto varias veces
-
20-12-2019 - |
Pregunta
estoy tratando de usar (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
para personalizar la visualización de PFObject
datos en un PFQueryTableViewController
.Entonces construyo mi PFQuery
objeto en (PFQuery *)queryForTable
en mi PFQueryTableViewController
delegate
que se supone que debe obtener los múltiples objetos en un NSArray
.
Pensé que se supone que parse.com luego envía/llama (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
una vez por cada uno de los objetos devueltos.Sin embargo, encuentro que sigue devolviendo el mismo objeto varias veces.
Así, por ejemplo, en lugar de 3 objetos diferentes para las 3 veces (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
se llama, obtengo el mismo objeto.Pero no es un problema de los datos ni de la consulta, porque al final de (PFQuery *)queryForTable
, justo antes
return query;
Lo intenté
[query findObjectsInBackgroundWithBlock:^(NSArray *resultsNow,NSError *error) {NSLog(@"We have the following: %@", resultsNow);}];
sleep(5);
y esto muestra los 3 objetos obtenidos correctamente.¿Cómo es que el exacto mismo consulta cuando es manejada por PFQueryTableViewController
vocación (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
3 veces, en lugar de enviar los 3 objetos uno por uno, envía el primero 3 veces?
En respuesta a las preguntas de Wain, agregué algunas respuestas en los comentarios, pero también lo siguiente parece relevante:
- (PFObject *)objectAtIndex:(NSIndexPath *)indexPath {
// overridden, since we want to implement sections
if (indexPath.section < self.objects.count) {
return [self.objects objectAtIndex:indexPath.section];
}
return nil;
}
Solución 2
Gracias a la pregunta de Wain, registré objetosDidLoad:y descubrí que se estaban cargando los 3 objetos correctos, por lo que tenía que ser una cuestión de que PFQueryTableViewController simplemente no los cargaba en la secuencia correcta en (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
.
En particular, se suponía que el mapeo estaba en -(PFObject *)objectAtIndex:(NSIndexPath *)indexPath
como se indica en mi pregunta.Pero al mirar la documentación por enésima vez, de repente me di cuenta de que el nombre del método debería ser -(PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
.No es de extrañar que no lo llamaran y que mi mapeo entrara en vigor y, por lo tanto, se pasara el objeto incorrecto a (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
.Cuando lo cambié a -(PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
, ¡El problema desapareció!
¿Cómo pude haber obtenido el nombre de método incorrecto?Descubrí que había copiado ese ejemplo de mapeo del código anypic, así que supongo que parse.com cambió el nombre del método en algún momento después de escribir anypic.¿Alguien puede verificar eso?Estoy usando el marco de análisis 1.2.14
Otros consejos
Tuve un problema similar y lo que hice fue:
1 - En lugar de usar PFQueryTableViewController
solo usa regular UITableViewController
.
2 - Cambiar el método queryForTable:
a (void)
.
- (void)queryForTable {
3 - Eliminar PFQueryTableView
métodos e implementar DataSource
métodos.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
Llamar [self queryForTable]
en viewDidLoad
después de declararlo en el archivo .h.
Si no usas bloquear, simplemente creas NSArray
property
y poblar en queryForTable
:
self.yourarray = [query findobjects];
Si usas bloquear, solo asegúrate de completar NSArray
bloque interior y recargar tableview.
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
// There was an error, do something with it.
}
else {
self.yourArray = objects;
}
// Reload your tableView Data
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
Así es como lo hice funcionar.Espero eso ayude.