我正在尝试从this url显示State(在标题标题中)和City(在表格行中)。热门城市将在第一部分进入,然后是州和城市。问题是我无法为分段州标题行分配相应的城市数组。这是我试过的......
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"Json dictionary = %@", json);
self.dictListSateMaster = [json objectForKey:@"RaffleInstanceLocations"];
for (NSDictionary *dict in [self.dictListSateMaster objectForKey:@"listStateMaster"])
{
ViewLocationsObjct *viewLocationsObjct = [[ViewLocationsObjct alloc] init];
viewLocationsObjct.StateNameStr = [dict objectForKey:@"StateName"];
[self.arrayListStateMaster addObject:[dict objectForKey:@"StateName"]];
for (NSDictionary *dictnry in [dict objectForKey:@"listCityMaster"])
{
//*********-- Need corrections here
ViewLocationStateMaster *viewLocNewObjct = [[ViewLocationStateMaster alloc] init];
viewLocNewObjct.CityName = [dictnry objectForKey:@"CityName"];
[self.arrayListCityMaster addObject:viewLocNewObjct];
}
}
for (NSDictionary *dict in [self.dictListSateMaster objectForKey:@"listTopCities"])
{
ViewLocationsObjct *viewLocationsObjct = [[ViewLocationsObjct alloc] init];
viewLocationsObjct.CityName = [dict objectForKey:@"CityName"];
[self.arrayTopCities addObject:viewLocationsObjct];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.arrayListStateMaster.count+1; //+1 to append to top cities section at the top
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cellId";
ViewLocationsTblCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (indexPath.section == 0)
{
ViewLocationsObjct *viewLocationObj = [self.arrayTopCities objectAtIndex:indexPath.row];
cell.locationLabl.text = viewLocationObj.CityName;
}
else
{
//*********-- Need corrections here
//NSString *sectionTitle = [self.arrayListStateMaster objectAtIndex:indexPath.section];
//NSMutableArray *arrayCities = [self.dictListSateMaster objectForKey:sectionTitle];
//cell.locationLabl.text = [arrayCities objectAtIndex:indexPath.row];
ViewLocationStateMaster *viewLoc = [self.arrayListCityMaster objectAtIndex:indexPath.row];
cell.locationLabl.text = viewLoc.CityName;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)//return top cities for 1st section
{
return self.arrayTopCities.count;
}
else
{
return self.arrayListCityMaster.count;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return @"Top Cities";
}
else
{
if (self.arrayListStateMaster.count > 0)
{
return [self.arrayListStateMaster objectAtIndex:section-1];//-1 to remove index of top cities section at the top
}
else
{
return @"";
}
}
}
答案 0 :(得分:-1)
好的,这就是我的问题如何解决了!
NSDictionary *dictRaffleInstLocations = [json objectForKey:@"RaffleInstanceLocations"];
sectionArray = [dictRaffleInstLocations objectForKey:@"listStateMaster"];
topCityArray = [dictRaffleInstLocations objectForKeyedSubscript:@"listTopCities"];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return topCityArray.count;
}else
return [[[sectionArray objectAtIndex:section-1]objectForKey:@"listCityMaster"] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionArray.count+1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"Top Cities";
}else
return [[sectionArray objectAtIndex:section-1]objectForKey:@"StateName"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"MyCellLocation";
ViewLocationsTblCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
{
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *titleString = @"";
if (indexPath.section == 0) {
titleString = [[topCityArray objectAtIndex:indexPath.row]objectForKey:@"CityName"];
}else{
NSArray *cityArray = [sectionArray[indexPath.section-1]objectForKey:@"listCityMaster"];
titleString = [cityArray[indexPath.row]objectForKey:@"CityName"];
}
cell.locationLabl.text = titleString;
return cell;
}