1

NSMutableDictionaryに取り込まれたJSONデータによって入力されたカスタムUITableViewがあります。次に、distanceFromLocationメソッドの値をディクショナリに追加できるように、可変コピーを作成します。私がやろうとしているのは、その新しいオブジェクトを使用して、セルを最も近い距離で並べ替えることです。NSSortDescriptorを使用する他の例を見てきましたが、それは配列のソートについて話しています。辞書からセルを並べ替えるか、辞書から配列を作成してNSSortDescriptorを使用するにはどうすればよいですか?

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

static NSString *CellIdentifier = @"dealerlistcell";

DealerListCell *cell = (DealerListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[DealerListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

//gets JSON data and loads into Dictionary
NSMutableDictionary *info = [json objectAtIndex:indexPath.row];
NSMutableDictionary *infocopy = [info mutableCopy];

//pulls the current user location
CLLocation *currentlocation2 = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];

//gets the latitude and longitude from info dictionary to calculate distancefromlocation for each cell
CLLocation *locationforindex = [[CLLocation alloc] initWithLatitude:[[info objectForKey:@"dlrlat"]doubleValue] longitude:[[info objectForKey:@"dlrlong"]doubleValue]];

//calculates the distance
CLLocationDistance dist = ([locationforindex distanceFromLocation:currentlocation2]*0.0006213711922);

//format the distance to a string for the label
NSString *distancestring = [NSString stringWithFormat:@"%.1f miles",dist];

//create object with distance to add to dictionary
NSNumber *distance = [NSNumber numberWithDouble:dist];

編集:後でNSSortDescriptorで呼び出すオブジェクトに割り当てられた距離

//add to dictionary 
[infocopy setObject:distance forKey:@"distance"];

//create array from dictionary
NSDictionary *aDict = [NSDictionary dictionaryWithDictionary:infocopy];
NSArray *anArray = [aDict allValues];

//implemented NSSortDescriptor
NSSortDescriptor *sorter [[NSSortDescriptor alloc] initWithKey:@"distance" ascending: YES];
NSArray *sortdescriptors = [NSArray arrayWithObject:sorter];
[anArray sortedArrayUsingDescriptors:sortdescriptors];

ただし、キー距離に対してvalueForUndefinedKeyをスローします。「initWithKey」で距離キーを定義するにはどうすればよいですか?

4

2 に答える 2

1

このフォーラムと友人の助けを借りて、このソリューションをつなぎ合わせることができました。

その答えは、テーブル ビューをロードする前に、for ループを使用し、メソッド内の各場所の辞書を作成することでした。次に、NSSortDescriptor を使用して並べ替えを行ったところ、完全に機能しました。

最終的な解決策については、以下のコードを参照してください。

-(void)getData:(NSData *) data 
{

NSError *error;

// load the jsonArray with the JSON Data
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];


//define the unsorted dealers array
unsortedDealers = [[NSMutableArray alloc] initWithCapacity:1000];

// start at row 0
int index = 0;

// loop through each dealer and calculate distance from current location
for (NSDictionary *dealer in jsonArray) 
{
    //create dictionary from JSON Array
    infoDict = [jsonArray objectAtIndex:index];

    //get the coordidates from current location
    CLLocation *currentlocation = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];

    //get the coordinates for dealer at row
    // dlr lat and longs are switched in the database
    CLLocation *locationforindex = [[CLLocation alloc] initWithLatitude:[[infoDict objectForKey:@"dlrlong"]doubleValue] longitude:[[infoDict objectForKey:@"dlrlat"]doubleValue]];

    //calculate the distance from current location to dealer at row and format it in miles
    CLLocationDistance dist = ([locationforindex distanceFromLocation:currentlocation]*0.0006213711922);

    //define the distance for display
    NSNumber *distanceForDict = [NSNumber numberWithDouble:dist];

    //define the distance for sorting
    NSString *distanceForSort = [NSString stringWithFormat:@"%.1f",dist];

   //create a dictionary for each dealer and define the values for each key 
   NSMutableDictionary *dealerDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[infoDict objectForKey:@"dlrname"] , @"dlrname", [infoDict objectForKey:@"dlrcity"], @"dlrcity", [infoDict objectForKey:@"dlrstate"] , @"dlrstate", [infoDict objectForKey:@"dlrzip"], @"dlrzip", [infoDict objectForKey:@"dlradd"] , @"dlradd", distanceForDict , @"dlrdistance" , distanceForSort , @"dlrsortdistance" , [infoDict objectForKey:@"dlremail"] , @"dlremail" , [infoDict objectForKey:@"dlrfax"] , @"dlrfax" , [infoDict objectForKey:@"dlrhours"] , @"dlrhours" , [infoDict objectForKey:@"dlrlat"], @"dlrlat",[infoDict objectForKey:@"dlrlong"] , @"dlrlong" , [infoDict objectForKey:@"dlrnum"], @"dlrnum",[infoDict objectForKey:@"dlrphone"] , @"dlrphone" , [infoDict objectForKey:@"dlrwebsite"], @"dlrwebsite", nil];

    //add dictionary to the unsorted array
    [unsortedDealers addObject:dealerDict];

    // iterate through the list of dealers
    ++index;

}

// creates the sorting element
NSSortDescriptor *sortDescriptor;

//define what you want to sort by
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dlrdistance" ascending:YES];

// build new array with thosed elements to be sorted
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

// create new array based on the sorted value
sortedDealers = [unsortedDealers sortedArrayUsingDescriptors:sortDescriptors];  

[self.tableView reloadData];

//error message if there is no data found. Usually this is an error with the connection, 
//it should never return empty
if (jsonArray ==  nil) 
{
    NSLog(@"No Data Loaded. Please check your internet connection");
}

}

于 2012-05-07T22:19:35.943 に答える
0

これを行う場合:

NSDictionary *aDict = [NSDictionary dictionaryWithDictionary:info];
NSArray *anArray = [aDict allValues];

NSSortDescriptor を使用して並べ替えることができる配列を返します。

于 2012-04-24T22:09:11.283 に答える