0

マップ上にポイントの配列があります。配列を正しく設定し、マップ上に注釈を付けます。すべてが正常に機能しています。同じ配列を持つ別のテーブルビューがあり、セルの「サブタイトル」で、ユーザーと現在までの距離を計算します。すべてが正常に機能しています。

ここで、テーブル ビューでリストを並べ替えます。つまり、同じ配列を距離で最低から最高に並べ替えたいと考えています。

問題は、距離が配列の一部ではないということです。では、距離を配列とクロスマッチさせるにはどうすればよいでしょうか。距離を並べ替えると、配列内の所属するオブジェクトが取得され、配列も並べ替えられますか?

私は ios にはかなり慣れていませんが、現在 3 つのアプリをリリースすることができました。これは 4 つ目のアプリで、はるかに複雑であり、これまでアプリを作成することでかなり良い基盤をカバーできたと思います。マップビューから、検索コントローラーとすべてを備えたテーブルビューまで。私はソートを欠いているだけです。

配列内の各オブジェクトにタグまたはプロパティを追加し、距離配列内の各距離に割り当てる必要があると思います。任意の提案をいただければ幸いです:)事前に感謝します。

4

2 に答える 2

3
// Assuming you have your points on the map in an NSArray called
// pointsOnMapArray and your distances in distanceArray, create a
// new mutable array to hold both.  Note, the "distances" in this
// case are stored as NSStrings.  We'll want to convert them to
// doubles before sorting them.
NSMutableArray *newArray = [[NSMutableArray alloc] init];

// Iterate over all of the points, and add a new element to the mutable
// array which is a new array containing a point and its distance.  The
// distance is converted from an NSString to an NSNumber containing a
// doubleValue.
int i;   
for (i = 0; i < pointsOnMapArray.count; i++) {
    NSArray *newItem = [NSArray arrayWithObjects: [pointsOnMapArray objectAtIndex: i], [NSNumber numberWithDouble:[[distanceArray objectAtIndex: i] doubleValue]], nil];
    [newArray addObject: newItem];
}

// Now, sort the new array based upon the distance in the second element
// of each array (ie, the distance).
[newArray sortUsingComparator: ^(id obj1, id obj2) {
    NSNumber *dist1 = [obj1 objectAtIndex:1];
    NSNumber *dist2 = [obj2 objectAtIndex:1];

    return [dist1 compare:dist2];
}];
于 2012-09-16T02:20:37.300 に答える
1

距離と array の要素で辞書を作成してみてください。次に、距離を並べ替えることで、配列要素をそれに応じて並べ替えることができます。

NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:(array element) forKey:(correspondingDistance)];

キーを並べ替えると、それに応じて配列の要素を並べ替えることができます。

于 2012-09-16T01:59:43.570 に答える