次のコードを使用して、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
これを実装する方法がわかりません。