// // DataTableViewController.m // Volta // // Created by Ronny Fenrich on 2013-06-14. // Copyright (c) 2013 Decoder. All rights reserved. // #import "DataTableViewController.h" #import "VoltaReading.h" #import "MGOrderedDictionary.h" #import "StatsTabBarController.h" @interface DataTableViewController () @property (strong, nonatomic) MGOrderedDictionary *data; // VoltaReading objects @end @implementation DataTableViewController - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatedDataNotification) name:NOTIFICATION_UPDATED_STATS_DATA object:nil]; } - (void)updatedDataNotification { [self.tableView reloadData]; } - (MGOrderedDictionary *)data { // get data from parent tabbar controller StatsTabBarController *parentTabBarController = (StatsTabBarController *)self.parentViewController; return parentTabBarController.data; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (!self.data) { return 1; // Loading } else if (self.data.allKeys.count == 0) { return 1; // no data } else { return self.data.allKeys.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (!self.data) { // still loading data UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoadingCell"]; return cell; } else if (self.data.count == 0) { // no results UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoResultsCell"]; return cell; } // Configure the data cell... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadingCell"]; NSString *dataKey = [self.data.allKeys objectAtIndex:indexPath.row]; VoltaReading *reading = [self.data objectForKey:dataKey]; UILabel *dateLabel = (UILabel *)[cell viewWithTag:1]; dateLabel.text = dataKey; UILabel *kWhLabel = (UILabel *)[cell viewWithTag:2]; float kWh = reading.usage / 1000.0f; kWhLabel.text = [NSString stringWithFormat:@"%.2f kWh", kWh]; UILabel *costLabel = (UILabel *)[cell viewWithTag:3]; float cost = reading.cost / 100000.0f; costLabel.text = [NSString stringWithFormat:@"%.2f $", cost]; return cell; } @end