简体   繁体   中英

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.

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.

When I use the following code, trying to populate the cell inside the for loop, it just adds four rows of the same item.

- (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.

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 . 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. Currently you're ignoring that argument entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM