0

I am having a major problem even getting started with finding an answer to this question? I have a UITableView that is being populated by user generated content and being persisted to CoreData. All is good. However I can't even find a way in the Apple docs to do the following:

How do I add each dollar value in a cell to get a grand total displayed somewhere to the user? In this case it can be thrown in a UILabel in the footer of each section I don't care. What I can't figure out is how to get to each individual cell get the dollar amount entered there and add it to the next dollar amount in the next cell in the section and so on.

Any ideas on how to add totals from cells to get a grand total?

4

1 回答 1

0

这个问题最优雅的解决方案是使用关键路径。

您无需前往单元格并获取数据。你去你的数据模型并从那里计算它。请记住,MVC 要求您始终将模型和视图分开。

假设您有一个 NSManagedObject 的子类,其 NSNumber 属性“数量”通过核心数据获取。然后,您可以轻松地进行如下简单的计算:

float sum = [[fetchedObjects valueForKeyPath:@"@sum.amount"] floatValue];

这在集合运算符下的键值编码编程指南中进行了描述。

于 2013-04-19T22:23:03.557 回答