【问题标题】:Populate tableview cells with data from NSArray / PFQuery使用来自 NSArray / PFQuery 的数据填充 tableview 单元格
【发布时间】:2019-05-29 23:40:55
【问题描述】:

我使用 parse.com 作为数据库,并且我在单元格中需要的数据似乎已正确传输到 nsarray,尽管我无法在我的表中显示它。

这是查询数据库的方法。

- (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>";

好的,作为大纲中的淋浴,我的查询成功地填充了一个数组,其中包含来自链接的数据库中不同表的四个对象。但我想这并不重要。

我需要做的是填充我的单元格,四行,因为我有四个带有特定键的项目。我想按行显示,分配给“titulo”和“Peso”的值似乎都在查询中正确返回。

当我使用以下代码尝试填充 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;
}

当我删除 for 循环并添加以下行时,我的表中没有任何内容。

object = [_exerciciosArray objectAtIndex:indexPath.row];
cell.textLabel.text = [object objectForKey:@"titulo"];

我已经尝试了很多东西,我敢肯定这是小事。请帮忙。

【问题讨论】:

    标签: ios objective-c uitableview parse-platform


    【解决方案1】:

    您正在设置单元 N 次,其中 N 是 _exerciciosArray.count。只有数组中的最后一项实际出现,因为它已分配给所有四个单元格。

    改变这个:

    for(PFObject *o in _exerciciosArray) {
        PFObject *object = o[@"exercicio"];
        cell.textLabel.text = [object objectForKey:@"titulo"];
    }
    

    到这里:

    PFObject *o = _exerciciosArray[indexPath.row];
    PFObject *object = o[@"exercicio"];
    cell.textLabel.text = object[@"titulo"];
    

    您需要根据传递给方法的indexPath 拉出不同的对象。目前你完全忽略了这个论点。

    【讨论】:

    • 完美!!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多