我对 iOS 比较陌生(如果我不清楚或者问题已在其他地方得到解决,我会事先道歉)并且我已经阅读了很多教程来学习基础知识。尽管如此,我在使用核心数据执行以下操作时遇到了问题:
- 我了解如何使用核心数据从数据库中获取数据、对其进行排序和显示,但现在我不知道如何以编程方式处理需要计算和显示的属性(它是我的数据库中表中的临时属性)在嵌入导航控制器的 tableview 控制器内的静态 tableview 单元格中。
举例说明:我有一个包含两个变量( InputValue (integer) 和 inputDate (NSDate) )的表,我想计算每年或每天输入的每个每日值的平均值/最小值/最大值。然后,这些值将在 tableview 静态单元格中显示和更新(每个单元格中的计算值。)
编辑:我在这里从 tableview 控制器添加了一些代码:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
//Configure the cell to show the user value as well as showing the date of input
PVMesswert *wert = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];
cell.textLabel.text= [[NSString alloc] initWithFormat:@"%@ kWh", wert.inputValue];
cell.detailTextLabel.text = [dateFormatter stringFromDate:wert.inputDate];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ //should I have a different cell identifier for each row where I want to display a result of the calculations?
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// Create and configure a fetch request with the PVMesswert entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"PVMessWert" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *datumsort = [[NSSortDescriptor alloc] initWithKey:@"inputDate" ascending:NO];
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"InputValue" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:datumsort, /*authorDescriptor,*/ nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"dateToString" cacheName:@"Root"];
_fetchedResultsController.delegate = self;
// Memory management.
return _fetchedResultsController;
}
因此,在我拥有的另一个表格视图中,我想显示如下内容:每年的平均/最大/最小输入值(基于属性输入日期 NSDat。)。我应该使用 NSExpression 吗?在这种情况下,如何使用?