2011-05-19 57 views
0

我写这篇文章的代码问题把数据的UITableView

- (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”信息。 这是我的控制器的.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 

我加入[self.tableView reloadData];和它的工作。 enter image description here

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

+0

你有没有设置[数据源](http://developer.apple.com/library /ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/dataSource)的表视图? – albertamg 2011-05-19 15:46:05

+0

不,但现在我试图添加@property(nonatomic,分配)ID 数据源;并在.m合成它,但仍然不起作用 – user567 2011-05-19 16:00:06

+0

也许你应该通过一些文档:http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html – vikingosegundo 2011-05-19 16:14:56

回答

1

要执行,你必须执行一重装:

[self.tableView reloadData]; 

在requestFinished结束:功能

+0

tahk你工作!! – user567 2011-05-19 16:08:24

+0

@ Mehdi:如果它的工作不要忘记投票和/或接受通知问题的答案已解决。 – 2011-05-19 16:15:06

0

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

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

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

+0

我不认为。我在文章中加入了controllrt.h – user567 2011-05-19 15:59:12

1

如果你已经正确设置了其他所有东西,你会尝试在- (void)requestFinished:(ASIHTTPRequest *)request的末尾执行reloadData吗?

+0

对不起,但我不知道如何执行重新加载:( – user567 2011-05-19 15:58:49