ios - 如何显示两组不同数据之间的平衡?
问题描述
我想在不同的 tableviewcontroller 上创建一个新的 table view 单元格,它将显示收入和支出数据之间的平衡。如何获取收入数据和费用数据的总和,从收入中减去费用,然后将该余额连接到新单元格中的 UILabel?
这是我的项目的布局方式:
- IncomeTableViewController - 列出收入的场景。
- AddEditIncomeTableViewController - 允许用户创建新收入的场景。
- IncomeTableViewCell - 有自定义单元格。一个 UILabel 为收入名称,另一个 UILabel 为收入金额。
- ExpenseTableViewController - 列出费用的场景。
- AddEditExpenseTableViewController - 允许用户创建新费用的场景。
- ExpenseTableViewCell - 有自定义单元格。一个 UILabel 用于费用名称,另一个 UILabel 用于费用金额。
import Foundation
struct Expense: Codable {
var name: String
var amount: String
init(name: String, amount: String) {
self.name = name
self.amount = amount
}
static let DocumentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("expenses").appendingPathExtension("plist")
static func loadSampleExpenses() -> [Expense] {
return [
Expense(name: "Rent/Mortgage", amount: "0"),
Expense(name: "Electric", amount: "0"),
Expense(name: "Gas", amount: "0"),
Expense(name: "Cell Phone", amount: "0"),
Expense(name: "Groceries", amount: "0"),
Expense(name: "Car Payment", amount: "0"),
Expense(name: "Auto Expenses", amount: "0"),
Expense(name: "Auto Insurance", amount: "0"),
Expense(name: "Personal Care", amount: "0"),
Expense(name: "Entertainment", amount: "0"),
Expense(name: "Miscellaneous", amount: "0")]
}
static func saveToFile(expenses: [Expense]) {
let propertyListEncoder = PropertyListEncoder()
let codedExpenses = try? propertyListEncoder.encode(expenses)
try? codedExpenses?.write(to: ArchiveURL, options: .noFileProtection)
}
static func loadFromFile() -> [Expense]? {
guard let codedExpenses = try? Data(contentsOf: ArchiveURL) else { return nil }
let propertyListDecoder = PropertyListDecoder()
return try? propertyListDecoder.decode(Array<Expense>.self, from: codedExpenses)
}
}
import Foundation
struct Income: Codable {
var name: String
var amount: String
init(name: String, amount: String) {
self.name = name
self.amount = amount
}
static let DocumentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("incomes").appendingPathExtension("plist")
static func loadSampleIncomes() -> [Income] {
return [
Income(name: "Main Income", amount: "0"),
Income(name: "Secondary Income", amount: "0"),
Income(name: "Interest Income", amount: "0")]
}
static func saveToFile(incomes: [Income]) {
let propertyListEncoder = PropertyListEncoder()
let codedIncomes = try? propertyListEncoder.encode(incomes)
try? codedIncomes?.write(to: ArchiveURL, options: .noFileProtection)
}
static func loadFromFile() -> [Income]? {
guard let codedIncomes = try? Data(contentsOf: ArchiveURL) else { return nil }
let propertyListDecoder = PropertyListDecoder()
return try? propertyListDecoder.decode(Array<Income>.self, from: codedIncomes)
}
}
我也有这两个文件,我在其中定义费用和收入。不确定需要显示什么代码。
解决方案
如果我理解正确,您希望拥有一个共享模型来保存数据,这样您就可以从不同的 UIViewControllers 中读取和写入模型中的数据。
最简单的方法是使用单例。
推荐阅读
- sql - Oracle:如何在表中的 USER_TABLES.TABLE_NAME 上添加外键
- c# - 一个对象项插入数组 Mongodb C#
- angular - PlatformRef.bootstrapModule(...) 引发错误,提示“未提供 ResourceLoader 实现”。
- html - html页面联系表格安全
- java - Liferay Genric Portlet Dem Null 值
- wordpress - 如何将 wordpress 类别网址(如 www.example.com/m/a1)修改为 www.example.com/category/m/a1
- php - 如何将带方括号的字符串转换为数组?
- c - 在c中存储指向带有参数函数的回调的指针
- react-native - 在平面列表中单独更改特定组件的状态,就像在 btns 之类的 facebook 中一样
- javascript - chrome 存储同步拆分错误