Ios NSMutableDictionary到tableview单元格
}Ios NSMutableDictionary到tableview单元格,ios,objective-c,uitableview,nsmutabledictionary,Ios,Objective C,Uitableview,Nsmutabledictionary,} 我从外部api获取航班数据,但如何让返回的航班在NSMutable字典中写入tableview单元格?我希望每个航班都能返回到自己的tableview单元格中。非常感谢您的帮助。NSLog您得到的JSON响应: NSString* path = @"http://username:apicode@flightxml.flightaware.com/json/FlightXML2/AirlineFlightSchedules?startDate=1394424000&endDate=1
我从外部api获取航班数据,但如何让返回的航班在NSMutable字典中写入tableview单元格?我希望每个航班都能返回到自己的tableview单元格中。非常感谢您的帮助。
NSLog
您得到的JSON
响应:
NSString* path = @"http://username:apicode@flightxml.flightaware.com/json/FlightXML2/AirlineFlightSchedules?startDate=1394424000&endDate=1394486000&origin=KLAX&destination=KJFK&airline=UAL&howMany=10&offset=0";
NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];
if(nil != error)
{
NSLog(@"Error: %@", error);
}
else
{
NSMutableDictionary* json = nil;
if(nil != _connectionData)
{
json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
}
if (error || !json)
{
NSLog(@"Could not parse loaded json with error:%@", error);
}
else
{
NSMutableDictionary *routeRes;
routeRes = [json objectForKey:@"AirlineFlightSchedulesResult"];
NSMutableArray *res;
res = [routeRes objectForKey:@"data"];
for(NSMutableDictionary *flight in res)
{
NSLog(@"ident is %@, aircrafttype is %@, originName is %@, origin is %@,%@,%@,%@,%@,%@,%@,%@", [flight objectForKey:@"actual_ident"], [flight objectForKey:@"aircrafttype"], [flight objectForKey:@"arrivaltime"], [flight objectForKey:@"departuretime"], [flight objectForKey:@"destination"], [flight objectForKey:@"ident"], [flight objectForKey:@"meal_service"], [flight objectForKey:@"origin"], [flight objectForKey:@"seats_cabin_business"], [flight objectForKey:@"seats_cabin_coach"], [flight objectForKey:@"seats_cabin_first"]);
}
}
_connectionData = nil;
NSLog(@"connection nil");
}
}
然后读我对你的回答。您所需要做的就是知道从外部API返回的内容的结构。您需要将
UITableView
数据源设置为航班字典数组。NSArray*flights=[res allValues]代码>
将为您获取一个包含数据的数组。
然后使用委托方法设置tableview。
例如
在-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
您可以:
NSMutableDictionary*flight=flights[indexPath.row];
cell.flightIdent.text=航班[@“ident];
假设您的单元格具有名为FlightIdent的UILabel属性。您正在获取JSON,然后对其进行解析。确定
你必须知道你的JSON布局,然后从结果对象中选取数据。看起来你正在做类似的事情。但是,你正在将结果分配给可变对象。你不能这样做,因为这些API返回不可变的对象
如果你想要一个可变版本,你需要调用mutableCopy
NSLog(@"json = %@", [json description]);
当然,这是假设您的JSON包含一个键“AirlineFlightSchedulesResult”的字典。没有为您执行显式检查。假设您知道您要的对象类型
然后,您只需要将数据转换为一个数组,在该数组中,表视图中所需的每个项都有一个数组项,并使用标准的表视图API
但是,从你的代码来看,它并不像你真的想要一个可变字典,你只是想把东西放进数组中。
NSMutableDictionary *routeRes;
routeRes = [[json objectForKey:@"AirlineFlightSchedulesResult"] mutableCopy];
现在,results
将是一个由MyFlight
对象组成的数组,每个对象都是通过工厂方法flightWithDictionary
通过以下几个步骤创建的:
创建UITableView,设置UITableViewDelegate和UITableViewDataSource
当您从外部api获取航班数据时,调用[YourTableView reloadData]
实现-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
实现-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
希望这对你有帮助。
谢谢!谢谢,但是当我添加数组my flight和最后一行代码时,我得到一个错误。NSArray的@interface不可见声明选择器FlightWith Dictionary我缺少什么?这是一些假设的MyFlight
类上的一个建议工厂方法。基本上,我假设您有一些类来表示航班……我只是假装类是MyFlight
,我进一步假设您将有一些工厂方法…flightWithDictionary
,该方法将使用字典作为输入值创建MyFlight
的实例。这就是您从事称为“编程”的罕见活动的地方。
NSMutableArray results = [NSMutableArray array];
NSDictionary *routeRes = [json objectForKey:@"AirlineFlightSchedulesResult"];
NSArray *res [routeRes objectForKey:@"data"];
for(NSDictionary *flight in res)
{
// Put whatever you want into your array.
[results addObject:[MyFlight flightWithDictionary:flight]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
NSArray *arAllFlights = [res allValues];
return [arAllFlights count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dicFlight = arAllFlights[indexPath.row];
cell.flightIdent.text = dicFlight[@"key"];
return cell.
}