2011-05-19 59 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