【问题标题】:How to do I display the balance between two different sets of data?如何显示两组不同数据之间的平衡?
【发布时间】:2020-04-20 17:19:22
【问题描述】:

我想在不同的 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)
    }

}

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

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

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

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

    【讨论】:

    • 我真的不在乎我是否有共享模型。我只想将收入金额数据和费用金额数据作为两者之间的平衡。我不知道该怎么做
    • 您在哪里保存收入和支出数据?
    猜你喜欢
    • 2016-12-12
    • 2016-12-31
    • 2020-06-07
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2018-05-20
    相关资源
    最近更新 更多