[英]Populate tableview cells with data from NSArray / PFQuery
I'm using parse.com as database, and the data that I need in the cell seems to be correctly transferred to an nsarray, although I can't show it in my table. 我正在使用parse.com作为数据库,尽管无法在表中显示该数据,但我在单元格中所需的数据似乎已正确传输到nsarray。
This is the method for querying database. 这是查询数据库的方法。
- (PFQuery *)queryForTable {
PFQuery *exerciciosQuery = [PFQuery queryWithClassName:@"ExerciciosPeso"];
[exerciciosQuery whereKey:@"usuario" equalTo:[PFUser currentUser]];
[exerciciosQuery includeKey:@"exercicio"];
// execute the query
_exerciciosArray = [exerciciosQuery findObjects];
for(PFObject *o in _exerciciosArray) {
PFObject *object = o[@"exercicio"];
NSLog(@"PFOBJECT %@", object);
NSLog(@"%@", o);
}
NSLog(@"%@", _exerciciosArray);
return exerciciosQuery;
}
Grupo = Biceps;
descricao = "descricao alternada";
titulo = "Rosca alternada";
Peso = 10;
exercicio = "<Exercicios:Iv2XB4EHSY>";
usuario = "<PFUser:W9ifgHpbov>";
Grupo = Biceps;
descricao = descricao;
titulo = "Puxada Reta";
Peso = 20;
exercicio = "<Exercicios:nmqArIngvR>";
usuario = "<PFUser:W9ifgHpbov>";
Grupo = Biceps;
descricao = "Fazer rosca";
titulo = "Rosca no Pulley";
Peso = 30;
exercicio = "<Exercicios:CXecX4DJiO>";
usuario = "<PFUser:W9ifgHpbov>";
Grupo = Biceps;
descricao = "em pe descricao";
titulo = "Biceps na corda";
Peso = 40;
exercicio = "<Exercicios:6slVOQnj3y>";
usuario = "<PFUser:W9ifgHpbov>";
Ok, as shower in the outline, my query successfully populates an array with four objects from different tables in the database which are linked. 好的,作为大纲中的淋浴,我的查询成功地使用来自数据库中不同表的四个对象(链接在一起)填充了一个数组。 But I guess this is not important.
但是我想这并不重要。
What I need to do is to populate my cells, four rows, as I have four items with specific keys. 我需要做的是填充我的单元格,四行,因为我有四个带有特定键的项。 I want to show per row, the values assigned to "titulo" and "Peso", both seem to be correctly returned in query.
我想每行显示分配给“ titulo”和“ Peso”的值,似乎都在查询中正确返回了。
When I use the following code, trying to populate the cell inside the for loop, it just adds four rows of the same item. 当我使用以下代码时,尝试填充for循环内的单元格,它只会添加同一项目的四行。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
for(PFObject *o in _exerciciosArray) {
PFObject *object = o[@"exercicio"];
cell.textLabel.text = [object objectForKey:@"titulo"];
}
return cell;
}
When I delete the for loop, and add the following lines, I don't get anything in my table. 当我删除for循环并添加以下行时,表中什么也没有。
object = [_exerciciosArray objectAtIndex:indexPath.row];
cell.textLabel.text = [object objectForKey:@"titulo"];
I have tried a lot of things already, I'm sure it is something small. 我已经尝试了很多东西,我敢肯定它很小。 Please help.
请帮忙。
You're setting up the cell N times, where N is _exerciciosArray.count
. 您正在设置单元N次,其中N是
_exerciciosArray.count
。 Only the last item in your array actually appears, since it's assigned to all four cells. 实际上,仅将数组中的最后一项显示出来,因为它已分配给所有四个单元格。
Change this: 更改此:
for(PFObject *o in _exerciciosArray) {
PFObject *object = o[@"exercicio"];
cell.textLabel.text = [object objectForKey:@"titulo"];
}
to this: 对此:
PFObject *o = _exerciciosArray[indexPath.row];
PFObject *object = o[@"exercicio"];
cell.textLabel.text = object[@"titulo"];
You need to pull out a different object depending on which indexPath
is passed to the method. 您需要根据将哪个
indexPath
传递给方法来拉出其他对象。 Currently you're ignoring that argument entirely. 当前,您将完全忽略该论点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.