2015-09-25 65 views
0

我想從this url顯示狀態(在節標題中)和城市(在錶行中)。熱門城市將進入第一部分,隨後是州和城市。問題是我無法爲分段的狀態標題行分配相應的城市陣列。這是我試過的..在iOS中解析JIT分段UITableView

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 @""; 
     } 

    } 
} 
+1

您需要一個陣列數組,其中內部陣列是給定狀態的城市。 – rmaddy

回答

-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; 
}