将数据从一个对象添加到 UITableView

发布于 2024-11-08 16:07:47 字数 1415 浏览 0 评论 0原文

我编写了这段代码,

NSDictionary *json    = [responseString JSONValue];
    Status *statut = [[Status alloc] init];
    statut.flyNumber = [json objectForKey:@"flynumber"];
    statut.ftatuts = [json objectForKey:@"fstatuts"];
    statut.escDepart = [json objectForKey:@"escdepart"];
    statut.escArrival = [json objectForKey:@"escarrival"];
    statut.proArrival = [json objectForKey:@"proarrival"];
    statut.proDepart = [json objectForKey:@"prodepart"];
    statut.estDepart = [json objectForKey:@"estdepart"];
    statut.estArrival = [json objectForKey:@"estarrival"];
    statut.realDepart = [json objectForKey:@"realdepart"];
    statut.realArrival = [json objectForKey:@"realarrived"];

    [dataToDisplay addObject:statut];
    [self.tableView reloadData];

我想将结果状态对象放在表视图中,每个属性在一行(单元格)中。 我不知道该怎么做。我写了这个,但他们在同一行中显示了它们。

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    NSLog(@"1 ok");
    Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];
    NSLog(@"2 ok");
    cell.textLabel.text =  [NSString stringWithFormat:@"%@%@",statut2.flyNumber,statut2.ftatuts];
    NSLog(@"3 ok");

    return cell;

请帮忙

i writed this code ,

NSDictionary *json    = [responseString JSONValue];
    Status *statut = [[Status alloc] init];
    statut.flyNumber = [json objectForKey:@"flynumber"];
    statut.ftatuts = [json objectForKey:@"fstatuts"];
    statut.escDepart = [json objectForKey:@"escdepart"];
    statut.escArrival = [json objectForKey:@"escarrival"];
    statut.proArrival = [json objectForKey:@"proarrival"];
    statut.proDepart = [json objectForKey:@"prodepart"];
    statut.estDepart = [json objectForKey:@"estdepart"];
    statut.estArrival = [json objectForKey:@"estarrival"];
    statut.realDepart = [json objectForKey:@"realdepart"];
    statut.realArrival = [json objectForKey:@"realarrived"];

    [dataToDisplay addObject:statut];
    [self.tableView reloadData];

and i want to put the result statut object in a table view , each attribute in a line ( cell ) .
i don't know how to to . I writed this but they show them in the same line .

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    NSLog(@"1 ok");
    Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];
    NSLog(@"2 ok");
    cell.textLabel.text =  [NSString stringWithFormat:@"%@%@",statut2.flyNumber,statut2.ftatuts];
    NSLog(@"3 ok");

    return cell;

Help please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-11-15 16:07:47

也许您可以让 Statut 类返回一个数组,其中包含您从 JSON 数据设置的所有属性。然后您可以简单地使用数组来设置行数和每行的单元格。

Maybe you could have your Statut class return an array containing all of the properties you set from your JSON data. Then you can simply use the array to set the number of rows and the cell for each row.

心碎的声音 2024-11-15 16:07:47

这是一个解决方案,允许您配置属性在 tableView 中显示的顺序:

// declare this enum in your .h file
enum {
    flyNumberRow   = 0, // first row in tableView
    ftatutsRow     = 1, // second row
    escDepartRow   = 2, // etc.
    escArrivalRow  = 3,
    proArrivalRow  = 4,
    proDepartRow   = 5,
    estDepartRow   = 6,
    estArrivalRow  = 7, 
    realDepartRow  = 8,
    realArrivalRow = 9

}

-tableView:cellForRowAtIndexPath: 方法实现中:

// ...
// initialize the cell as you did in your code already

Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];

switch(indexPath.row) {
    case flyNumberRow:
        cell.textLabel.text =  [NSString stringWithFormat:@"%@",statut2.flyNumber];
        break;
    case ftatutsRow:
        cell.textLabel.text =  [NSString stringWithFormat:@"%@",statut2.ftatuts];
        break;
    // ... and so on for each case
}

return cell;

显然,tableView:numberOfRowsInSection: 必须返回10

Here is a solution that allows you to configure the order in which your attributes will appear in your tableView:

// declare this enum in your .h file
enum {
    flyNumberRow   = 0, // first row in tableView
    ftatutsRow     = 1, // second row
    escDepartRow   = 2, // etc.
    escArrivalRow  = 3,
    proArrivalRow  = 4,
    proDepartRow   = 5,
    estDepartRow   = 6,
    estArrivalRow  = 7, 
    realDepartRow  = 8,
    realArrivalRow = 9

}

In your -tableView:cellForRowAtIndexPath: method implementation:

// ...
// initialize the cell as you did in your code already

Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];

switch(indexPath.row) {
    case flyNumberRow:
        cell.textLabel.text =  [NSString stringWithFormat:@"%@",statut2.flyNumber];
        break;
    case ftatutsRow:
        cell.textLabel.text =  [NSString stringWithFormat:@"%@",statut2.ftatuts];
        break;
    // ... and so on for each case
}

return cell;

Evidently, tableView:numberOfRowsInSection: must return 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文