미리보기 :
참고 : 나는 스위프트 3 사용하고 있습니다.
의 ViewController :
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?
}
모든 날짜는 동시에에게'0시 0분 0초 UTC'를 포함? –
@NDoc 예, 감사합니다. –