我可以解析数据并查看输出,但我无法在表格视图中显示它们
问题是第一个我的tableviews代码正在执行然后休息函数正在工作,这就是为什么我得到0,如何避免这个?
这是我的代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@" value %d",[jsonArray count]);// always 0
return [jsonArray count];
}
在这种方法中我也得到0值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath { NSDictionary *aTweet = [jsonArray objectAtIndex:[indexPath
row]]; NSLog(@ " divy is
%@",aTweet);// nothing
}
其他代码
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *data = (NSDictionary *) [json objectWithString:responseString error:nil];
NSDictionary *menu = (NSDictionary *) [data objectForKey:@"name"];
//label.text=[dict objectForKey:@"firstName"];
// parsing all the items in to the NSArray
NSArray *items = (NSArray *) [menu objectForKey:@"phoneNumber"];
int ndx;
//NSDictionary *stream;
for (ndx = 0; ndx< items.count; ndx++) {
stream = (NSDictionary *)[items objectAtIndex:ndx];
NSLog(@"This is the title of a stream: %@", [stream valueForKey:@"type"]);
[jsonArray addObject:stream];
}
NSLog(@" aray is %@",jsonArray);// show all data working
NSLog(@" sterab is %@",[stream valueForKey:@"home"]);// this is also working
这是我的观点,载荷()
- (void)viewDidLoad {
jsonArray= [[NSMutableArray alloc] init] ;
[super viewDidLoad];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://Json/Number.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}
答案 0 :(得分:2)
问题可能是您的数据没有立即加载。而UITableView
在创建时会将必要的数据存储在某种缓存中。这样可以避免经常调用数据源的方法,如tableView:numberOfRowsInSection:
和其他方法。因此,您应该明确调用UITableView
的{{1}}方法来刷新表。
要解决这个问题,你应该这样做:
reloadData
中只显示一些微调器以指示程序活动。 viewDidLoad
,请致电connectionDidFinishLoading:
。