UITableview中可扩展和可折叠节的数据结构

时间:2019-07-08 12:43:11

标签: ios swift uitableview realm

我有一个UITableViewController,其中显示了按日期排序的交易列表。这是用于个人理财/银行风格的应用程序。

我要实现的目标:点击时,标题部分(显示日期)应该展开/折叠。

我正在使用Realm数据库存储交易数据。

我是一名初学者程序员,我曾尝试通过Google搜索解决方案,但仍坚持尝试创建数据结构来跟踪某个节是打开还是关闭。

我已尝试遵循此(https://www.youtube.com/watch?v=Q8k9E1gQ_qg&),但在12:00被卡住。

领域模型:


class Transaction: Object {
    @objc dynamic var amount: Float = 0
    @objc dynamic var toFrom: String = ""
    @objc dynamic var category : String = ""
    @objc dynamic var date: Date? = nil
    @objc dynamic var note: String = ""
}

表视图控制器:


class TransactionsViewController : UITableViewController {

    @IBOutlet var transactions2TableView: UITableView!

    let realm = try! Realm()

    var groupedTransactions = [Date:Results<Transaction>]()
    var transactionDates = [Date]()

    override func viewDidLoad() {

        super.viewDidLoad()
        loadTransactions()
    }

override func numberOfSections(in tableView: UITableView) -> Int { 
        return transactionDates.count
    }

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return groupedTransactions[transactionDates[section]]!.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)
        let itemsForDate = groupedTransactions[transactionDates[indexPath.section]]!

        cell.textLabel?.text = "\(Array(itemsForDate)[indexPath.row].toFrom)"
        cell.detailTextLabel?.text = "\(String(describing: convertToCurrency(Array(itemsForDate)[indexPath.row].amount)!))"

        return cell

    }

func loadTransactions() {

        let transaction = realm.objects(Transaction.self).sorted(byKeyPath: "date", ascending: false)

        //Find each unique day for which a Transaction exists in your Realm
        transactionDates = transaction.reduce(into: [Date](), { results, currentTransaction in
            let date = currentTransaction.date!
            let beginningOfDay = Calendar.current.date(from: DateComponents(year: Calendar.current.component(.year, from: date), month: Calendar.current.component(.month, from: date), day: Calendar.current.component(.day, from: date), hour: 0, minute: 0, second: 0))!
            let endOfDay = Calendar.current.date(from: DateComponents(year: Calendar.current.component(.year, from: date), month:     Calendar.current.component(.month, from: date), day: Calendar.current.component(.day, from: date), hour: 23, minute: 59, second: 59))!

            //Only add the date if it doesn't exist in the array yet
            if !results.contains(where: { addedDate->Bool in
                return addedDate >= beginningOfDay && addedDate <= endOfDay
            }) {
                results.append(beginningOfDay)
            }
        })

        //Filter each transaction in realm based on their date property and assign the results to the dictionary
        groupedTransactions = transactionDates.reduce(into: [Date:Results<Transaction>](), { results, date in
            let beginningOfDay = Calendar.current.date(from: DateComponents(year: Calendar.current.component(.year, from: date), month: Calendar.current.component(.month, from: date), day: Calendar.current.component(.day, from: date), hour: 0, minute: 0, second: 0))!
            let endOfDay = Calendar.current.date(from: DateComponents(year: Calendar.current.component(.year, from: date), month: Calendar.current.component(.month, from: date), day: Calendar.current.component(.day, from: date), hour: 23, minute: 59, second: 59))!
            results[beginningOfDay] = realm.objects(Transaction.self).filter("date >= %@ AND date <= %@", beginningOfDay, endOfDay)
        })


        self.transactionsTableView.reloadData()
    }

0 个答案:

没有答案