这段代码:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSMutableDictionary *theDictionary = [responseString JSONValue];
NSLog(@"%@", theDictionary);
[responseString release];
if (theDictionary != nil)
{
NSArray *routesArray = [theDictionary objectForKey:@"routes"];
NSDictionary *firstRoute = [routesArray objectAtIndex:0];
NSArray *legsArray = [firstRoute objectForKey:@"legs"];
NSDictionary *firstLeg = [legsArray objectAtIndex:0];
steps = [firstLeg objectForKey:@"steps"];
}
[tableView reloadData];
}
我想在uitableview上显示每一个@“html_instructions”。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.steps count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];
if (cell == nil)
cell = [[[UITableViewCell alloc]
initWithFrame:CGRectZero reuseIdentifier:@"cachedCell"] autorelease];
return cell;
}
我的代码中缺少什么?应该是一些小细节......任何帮助都会很棒!
答案 0 :(得分:1)
我会这样做: 使用您的网址创建网址请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:@"http://maps.google.com.br/maps/api/directions/json?origin=porto+alegre&destination=novo+hamburgo&sensor=false"]];
然后创建NSData结构,该结构保存对请求的响应,并将其转换为字符串
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //Or async request
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
之后,将JSON加载到Dictionary中,然后向下移动一级,将其传递给另一个字典(结果1)
NSError *error=nil;
result = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
result1 = [result objectForKey:@"routes"];
result2 = [result1 objectForKey:@"legs"];
然后将其移至NSMutableArray
jsonMutArray= [result2 objectForKey:@"steps"];
从那里,您只需将标签放入自定义单元格中,将其作为IBOutlet附加,合成并执行cellForRowAtIndexPath方法
cell.yourLabel.text = [jsonMutArray objectForKey:@"html_instructions"];
然而,请记住,我只是打算,所以这可能不是最有效的方法:)祝你好运!
答案 1 :(得分:0)
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/可能这篇文章会对你有所帮助..
如果阅读文章后出现任何问题,请敲我。:)
答案 2 :(得分:0)
你不需要在你的CellForRowAtIndexPath中做这样的事情吗?
NSString *cellValue = **WHATEVER VALUE YOU WANT TO PUT IN**;
cell.textLabel.text = cellValue;
return cell;
我没有看到您将数据添加到单元格。