首页 > 解决方案 > 如何显示两组不同数据之间的平衡?

问题描述

在此处输入图像描述 我想在不同的 tableviewcontroller 上创建一个新的 table view 单元格,它将显示收入和支出数据之间的平衡。如何获取收入数据和费用数据的总和,从收入中减去费用,然后将该余额连接到新单元格中的 UILabel?

这是我的项目的布局方式:

  1. IncomeTableViewController - 列出收入的场景。
  2. AddEditIncomeTableViewController - 允许用户创建新收入的场景。
  3. IncomeTableViewCell - 有自定义单元格。一个 UILabel 为收入名称,另一个 UILabel 为收入金额。
  4. ExpenseTableViewController - 列出费用的场景。
  5. AddEditExpenseTableViewController - 允许用户创建新费用的场景。
  6. 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)
    }

}

我也有这两个文件,我在其中定义费用和收入。不确定需要显示什么代码。

标签: iosswiftxcodeuitableview

解决方案


如果我理解正确,您希望拥有一个共享模型来保存数据,这样您就可以从不同的 UIViewControllers 中读取和写入模型中的数据。

最简单的方法是使用单例。


推荐阅读