对象数组中的 UITableView 部分 (Swift)

M K*_*acs 1 object uitableview sections swift

I\xe2\x80\x99ve 得到了一个 Transaction 对象数组:

\n\n
var transactions = [Transaction]()\n
Run Code Online (Sandbox Code Playgroud)\n\n

交易类别:

\n\n
class Transaction {\n    var description : String = ""\n    var post_transaction_balance : Double = 0\n    var settlement_date : NSDate?\n    var dateOnly : NSDate?\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我需要创建一个包含部分的 UITableView,每个部分代表特定日期进行的交易

\n\n

表结构的示例如下:

\n\n

\xe2\x80\x94 节头:dateOnly\xe2\x80\x94

\n\n

交易[0]标题

\n\n

交易[3]标题

\n\n

\xe2\x80\x94 节头:dateOnly\xe2\x80\x94

\n\n

交易[2]标题

\n\n

交易[7]标题

\n\n

仅日期值示例:

\n\n
dateOnly = (NSDate?) 2016-01-22 00:00:00 UTC\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不确定如何迭代对象数组并将数据放入节标题和单元格中。我将非常感谢任何帮助。

\n

Wil*_*son 5

结果预览:

在此输入图像描述

注意:我使用的是 Swift 3。

视图控制器:

class ViewController: UIViewController {
  @IBOutlet weak private var tableView: UITableView!
  var transactionsGroupedByDate = [(String,Array<Transaction>)]()

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    getTransactions()
  }

  // Getting transactions
  private func getTransactions() {
    let transactions = makeTransactions()
    self.transactionsGroupedByDate = groupByDate(transactions: transactions)

    tableView.reloadData()
  }

  // Grouping the transactions by their date
  private func groupByDate(transactions: [Transaction]) -> [(String,Array<Transaction>)] {
    var transactionsGroupedByDate = Dictionary<String, Array<Transaction>>()

    // Looping the Array of transactions
    for transaction in transactions {

      // Converting the transaction's date to String
      let date = convertDateToString(date: transaction.date!)

      // Verifying if the array is nil for the current date used as a
      // key in the dictionary, if so the array is initialized only once
      if transactionsGroupedByDate[date] == nil {
        transactionsGroupedByDate[date] = Array<Transaction>()
      }

      // Adding the transaction in the dictionary to the key that is the date
      transactionsGroupedByDate[date]?.append(transaction)
    }

    // Sorting the dictionary to descending order and the result will be
    // an array of tuples with key(String) and value(Array<Transaction>)
    return transactionsGroupedByDate.sorted { $0.0 > $1.0 }
  }
}
Run Code Online (Sandbox Code Playgroud)

辅助方法:

extension ViewController {
  // Helper to create a date formatter
  func createDateFormatter() -> DateFormatter {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateFormatter.timeZone = TimeZone(identifier: "UTC")

    return dateFormatter
  }

  // Helper to convert date to string
  func convertDateToString(date: Date) -> String {
    let dateFormatter = createDateFormatter()

    return dateFormatter.string(from: date)
  }

  // Mocking the transactions
  func makeTransactions() -> [Transaction] {
    var transactions = [Transaction]()

    let dateFormatter = createDateFormatter()

    let date1 = dateFormatter.date(from: "2016-01-22 00:00:00")
    let transaction1 = Transaction(title: "transaction 1", date: date1)

    let date2 = dateFormatter.date(from: "2016-01-22 00:00:00")
    let transaction2 = Transaction(title: "transaction 2", date: date2)

    let date3 = dateFormatter.date(from: "2016-01-23 00:00:00")
    let transaction3 = Transaction(title: "transaction 3", date: date3)

    let date4 = dateFormatter.date(from: "2016-01-24 00:00:00")
    let transaction4 = Transaction(title: "transaction 4", date: date4)

    let date5 = dateFormatter.date(from: "2016-01-24 00:00:00")
    let transaction5 = Transaction(title: "transaction 5", date: date5)

    let date6 = dateFormatter.date(from: "2016-01-25 00:00:00")
    let transaction6 = Transaction(title: "transaction 6", date: date6)

    transactions.append(transaction1)
    transactions.append(transaction2)
    transactions.append(transaction3)
    transactions.append(transaction4)
    transactions.append(transaction5)
    transactions.append(transaction6)

    return transactions
  }
}
Run Code Online (Sandbox Code Playgroud)

UITableViewDelegate、UITableViewDataSource 方法:

extension ViewController: UITableViewDelegate, UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return transactionsGroupedByDate[section].1.count
  }

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

  func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return transactionsGroupedByDate[section].0
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    let transaction = transactionsGroupedByDate[indexPath.section].1[indexPath.row]

    cell.textLabel?.text = transaction.title

    return cell
  }
}
Run Code Online (Sandbox Code Playgroud)

交易结构:

struct Transaction {
  var title: String?
  var date: Date?
}
Run Code Online (Sandbox Code Playgroud)