2014-12-30 24 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]訪問索引條目,因爲美國不再是一個數組,但鑰匙在頂級字典中。

希望有所幫助。