将数据放入 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”消息。 这是我的控制器。
#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 .
But how to put each result on a line (cell) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要执行重新加载,您必须
在 requestFinished: 函数末尾执行 :
To execute a reload you have to execute :
at the end of the requestFinished: function
如果您已正确设置其他所有内容,您会尝试在
- (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
?您的控制器(?)是否符合
UITableViewDelegate
和UITableViewDataSource
协议?如果是,请检查并确保您已设置
dataSource
属性。通常,它被设置为self
。Does your controller (?) conform to the
UITableViewDelegate
andUITableViewDataSource
protocols?If yes, check to make sure you've set the
dataSource
property. Typically, this is set toself
.