【问题标题】:UITableView Sections from an array of Objects (Swift)对象数组中的 UITableView 部分 (Swift)
【发布时间】:2016-09-12 15:16:37
【问题描述】:

我有一个 Transaction 对象数组:

var transactions = [Transaction]()

事务类:

class Transaction {
    var description : String = ""
    var post_transaction_balance : Double = 0
    var settlement_date : NSDate?
    var dateOnly : NSDate?
 }

我需要创建一个带有部分的 UITableView,每个部分代表特定日期的交易

表结构的示例:

— 部分标题:dateOnly—

交易[0]标题

交易[3]标题

— 部分标题:dateOnly—

交易[2]标题

交易[7]标题

dateOnly 值示例:

dateOnly = (NSDate?) 2016-01-22 00:00:00 UTC

我不确定如何遍历对象数组并将数据放入节标题和单元格中。我将不胜感激任何帮助。

【问题讨论】:

  • 所有日期都包含相同的时间00:00:00 UTC ?
  • @NDoc 是的,谢谢

标签: swift uitableview object sections


【解决方案1】:

结果预览:

注意:我使用的是 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 }
  }
}

辅助方法:

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
  }
}

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
  }
}

交易结构:

struct Transaction {
  var title: String?
  var date: Date?
}

【讨论】:

    【解决方案2】:

    为什么不将数组重新组织成字典。

    您可以枚举事务数组并将其保存为 [String: Transaction]。关键可能是您对特定日期的描述。

    【讨论】:

      【解决方案3】:

      您需要有一个section 数组(即[Int:NSDate]),其中包含每个部分的起始索引和每个部分的日期值(部分标题)。

      您可以定义一个 func 填充部分数组,并可以在其中执行以下操作:

      1. 根据其中的dateOnly 对象对transactions 数组重新排序,以便日期最近的Transaction 对象位于顶部。

      2. 迭代排序后的数组,在数组中找到与前一个对象不同的dateOnly对象,并将这些dateOnly对象放入section数组中。

      现在,您有一个包含 dateOnly 对象的 section 数组和一个已排序的 Transaction 数组。您可以轻松地使用这些来填充tableView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        相关资源
        最近更新 更多