I am working in Swift to try and make a list view that shows all the transactions connected from all the accounts using Plaid API. But when I run this I get nothing. I have tried several different things, but nothing is working.
import UIKit
class TransactionViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
let communicator = ServerCommunicator()
var transactions: [Transaction] = []
override func viewDidLoad() {
super.viewDidLoad()
title = "Transactions"
tableView.dataSource = self
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TransactionCell")
fetchTransactions()
}
private func fetchTransactions() {
communicator.callMyServer(path: "/server/transactions/get", httpMethod: .get) { (result: Result<[Transaction], ServerCommunicator.Error>) in
DispatchQueue.main.async {
switch result {
case .success(let fetchedTransactions):
print("Fetched transactions: \(fetchedTransactions)") // Debug: Print fetched transactions
self.transactions = fetchedTransactions
self.tableView.reloadData()
case .failure(let error):
print("Failed to fetch transactions: \(error)") // Debug: Print error if fetch fails
}
}
}
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Number of transactions: \(transactions.count)") // Debug: Print number of rows in the table
return transactions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TransactionCell", for: indexPath)
let transaction = transactions[indexPath.row]
// Debug: Print transaction data for each row
print("Transaction at index \(indexPath.row): \(transaction.date), \(transaction.description), $\(transaction.amount)")
cell.textLabel?.text = "\(transaction.date): \(transaction.description) - $\(transaction.amount)"
cell.detailTextLabel?.text = transaction.accountName
return cell
}
}