2014-12-30 21 views
-2

我有一个nsmutable数组,返回一个公司地址。我目前有数组返回所有的状态。但我希望它只返回每个国家之一。
当前实例:Ca.娃。娃。糜。约当我选择一个状态时,我需要将信息带到下一页。这是我的代码到目前为止。xcode循环显示地址列表中的每个状态只有一个

- (NSArray *)readCompanies:(NSURL *)url { 
    //create a nsurlrequest with the given Url 
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy: 
          NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0]; 

    //get the data 
    NSURLResponse *response; 
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 

    //now create a nsdictionary from the json data 
    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data 
                    options:0 error:nil]; 

    //create a new array to hold the comanies 
    NSMutableArray *companies = [[NSMutableArray alloc] init]; 

    //get an array of dictionaries with the key "company" 
    NSArray *array = [jsonDictionary objectForKey:@"companies"]; 

    //iterate throught the array of dictionaries 
    for (NSDictionary *dict in array) { 
     //create a new company object with information in the dictionary 
     Company *company = [[Company alloc] initWithJSONDictionary:dict]; 
     //add the Company object to the array 
     [companies addObject:company]; 

    } 

    //return the array of Company objects 
    return companies; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


#pragma mark -table view controller methods 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { 
    return [companies count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellID = @"CellIDState"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID]; 

    if (cell == nil){ 
     //single line on table view 
     //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]; 
     // dual line on table view 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; 
    } 

    Company *company = [companies objectAtIndex:indexPath.row]; 

    //cell.textLabel.text = company.company_id; 
    cell.textLabel.text = company.state; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",company.companyName]; 
    //adds cheveron to tableviewl 
    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator]; 

    return cell; 
} 

感谢您的帮助。

回答

0

看着你的代码,你有一个公司对象的NSMutableArray。然后,您将显示一张带有每个公司状态按钮的表格。

Company 1, CA -> [CA] 
Company 2, WA -> [WA] 
Company 3, CA -> [CA] 

如果我正确理解您的问题,您只希望显示每个有公司的州的单个单元格。这意味着您需要一种方式来代表每个州的多家公司。

这样做的一种可能性是将顶级结构作为字典使用,其中键是状态,值是公司在该状态下的数组。上述三个公司将在此结构中最终成为:

NSDictionary 
    key1 (CA) 
    Company 1 
    Company 3 
    key2 (WA) 
    Company 2 

的代码做,这是不是你做的循环稍微复杂一些。该readCompanies它应该是这样的(请原谅我的粗略伪代码):

-(NSDictionary*) readCompanies(NSURL* url) { 
    Get URL data 
    Parse URL data into array of dictionaries (you have these steps already) 
    NSDictionary* companyByStates = [[NSDictionary alloc] init]; 

    //iterate throught the array of dictionaries 
    for (NSDictionary *dict in array) { 
    //create a new company object with information in the dictionary 
    Company *company = [[Company alloc] initWithJSONDictionary:dict]; 

    if companyByStates does not have entry for key(company->state) { 
     create new NSMutableArray 
     add this company to new array 
     add array to companyByStates under key (company->state) 
    } 
    else // already have entry for that state 
    { 
     get array for that state: companyByStates[company->state] 
     add this company to the array for that state 
    } 
    } 
    return companyByStates 
} 

此外,您tableView方法需要使用一个[NSDictionary keyEnumerator]访问索引条目,因为美国不再是一个数组,但钥匙在顶级字典中。

希望有所帮助。