将数据放入 UITableView 中的问题

发布于 2024-11-08 19:18:03 字数 2545 浏览 1 评论 0原文

我写这段代码的

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];

    dataToDisplay = [[NSMutableArray alloc] init];
    //NSString *myJSON = [[NSString alloc] initWithString:responseString];

    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];
    NSLog(@"ok");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [dataToDisplay count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    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;
}

问题是表仍然是空的。而且我的控制台中没有“ok 1”消息。 这是我的控制器。

#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "JSON.h"
#import "Status.h"

@interface StatusTableViewController : UITableViewController {
    NSString * Flynumber;   
    NSMutableArray *dataToDisplay;
}
@property(retain,nonatomic) IBOutlet NSString * Flynumber;
@end

我添加了 [self.tableView reloadData]; 并且它可以工作。 在此处输入图像描述

但是如何将每个结果放在一行(单元格)上?

i write this code

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];

    dataToDisplay = [[NSMutableArray alloc] init];
    //NSString *myJSON = [[NSString alloc] initWithString:responseString];

    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];
    NSLog(@"ok");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [dataToDisplay count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    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;
}

The problem that the table still empty . And i don't have "ok 1" message in the console .
This is my controller .h

#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "JSON.h"
#import "Status.h"

@interface StatusTableViewController : UITableViewController {
    NSString * Flynumber;   
    NSMutableArray *dataToDisplay;
}
@property(retain,nonatomic) IBOutlet NSString * Flynumber;
@end

i added [self.tableView reloadData]; and it work .
enter image description here

But how to put each result on a line (cell) ?

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

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

发布评论

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

评论(3

恍梦境° 2024-11-15 19:18:03

要执行重新加载,您必须

[self.tableView reloadData];

在 requestFinished: 函数末尾执行 :

To execute a reload you have to execute :

[self.tableView reloadData];

at the end of the requestFinished: function

舂唻埖巳落 2024-11-15 19:18:03

如果您已正确设置其他所有内容,您会尝试在 - (void)requestFinished:(ASIHTTPRequest *)request 末尾执行 reloadData 吗?

provided you have set everything else correctly, would you try and execute a reloadData at the end of - (void)requestFinished:(ASIHTTPRequest *)request?

蓬勃野心 2024-11-15 19:18:03

您的控制器(?)是否符合 UITableViewDelegateUITableViewDataSource 协议?

@interface YourController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}

如果是,请检查并确保您已设置 dataSource 属性。通常,它被设置为self

Does your controller (?) conform to the UITableViewDelegate and UITableViewDataSource protocols?

@interface YourController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}

If yes, check to make sure you've set the dataSource property. Typically, this is set to self.

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