1

次のコードを使用して、plist を使用して UITableView を設定しています。

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Items" ofType:@"plist"];
NSDictionary *itemDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.itemList = itemDictionary[@"List of Items"];

plist は次のようになります。

<key>List of Items</key>
<array>
    <<dict>
        <key>Name</key>
        <string>Name of Item 1</string>
        <key>Description</key>
        <string>Description of Item 1</string>
        <key>Latitude</key>
        <integer>0</integer>
        <key>Longitude</key>
        <integer>0</integer>
    </dict>
    <dict>
        <key>Name</key>
        <string>Name of Item 2</string>
        <key>Description</key>
        <string>Description of Item 2</string>
        <key>Latitude</key>
        <integer>0</integer>
        <key>Longitude</key>
        <integer>0</integer>
    </dict>

以下を使用して、セル内の各アイテムのタイトルとサブタイトル (座標からの距離) を設定できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

            cell.textLabel.text = self.itemList[indexPath.row][@"Name"];
            CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[self.itemList[indexPath.row][@"Latitude"] doubleValue]
                                                           longitude:[self.itemList[indexPath.row][@"Longitude"] doubleValue]];
            CLLocationDistance itemDistance = [itemLoc distanceFromLocation:currentLocation];
            cell.detailTextLabel.text = [[NSString alloc] initWithFormat: @"%.f metres", itemDistance];
return cell;
 }

ただし、テーブル ビューを最も近い場所 (アイテム) で並べ替え/並べ替えたいと考えています。itemDistanceこれは、浮動小数点値または全体を使用して実行できる可能性がありますが、detailTextLabel.textこれを実装する方法がわかりません。

4

3 に答える 3

1

tableviewcells に含まれるデータに基づいて tableview を単純にソートすることはできません (表示されている tableview セルのみが存在します)。あなたの場合、self.itemListのテーブルビューのデータソースをソートする必要があります。self.itemList 内の各アイテムの距離を計算する新しいメソッドを作成し、その距離に基づいて self.itemList をソートするか、ソートされた値で新しい配列を作成します。その値でソートしたい場合は、 cellforrowatindexpath メソッドで距離を計算しないでください。

于 2013-10-16T22:16:48.377 に答える
0

「NSSortDiscriptor」を見てみてください。

サンプル:

NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
NSArray *sortDescriptors = @[ageDescriptor];
NSArray *sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];
于 2013-10-16T22:17:34.307 に答える