2016-09-12 2 views
1

나는 트랜잭션 객체의 배열을 가지고 섹션은 특정 날짜에 수행 된 트랜잭션을 나타냅니다.jQuery과 섹션

,210

- 섹션 헤더 : dateOnly-

트랜잭션 [0] 표제

트랜잭션 [3] 표제

- 섹션 헤더 : dateOnly-

트랜잭션 [2] 표제

트랜잭션 [7] 제목

날짜의 예 유일한 값 :

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

개체 배열을 반복하고 데이터를 구역 머리글과 셀로 가져 오는 방법을 모르겠습니다. 어떤 도움을 주시면 감사하겠습니다.

+0

모든 날짜는 동시에에게'0시 0분 0초 UTC'를 포함? –

+0

@NDoc 예, 감사합니다. –

답변

0

배열을 사전으로 재구성하지 않는 이유?

트랜잭션 배열을 열거하고 [String : Transaction]으로 저장할 수 있습니다. 열쇠는 당신의 특별한 날의 묘사 일 수 있습니다.

0

각 섹션의 시작 색인이 포함 된 section 배열 (즉, [Int:NSDate])이 있어야합니다. 각 섹션의 날짜 값 (섹션 제목)은 &입니다.

당신은 섹션 배열을 채우는 func을 정의 할 수 있고, 그 안에 다음을 수행 할 수 : 가장 가까운 날짜와 함께 Transaction 객체가 될 수 있도록

  1. 는, 거기에 dateOnly 목적에 따라 transactions 배열 순서 변경을 상단에.

  2. 정렬 된 배열을 반복하여 배열의 이전 개체와 다른 개체 인 을 찾고 섹션 배열에 dateOnly 개체를 넣습니다.

지금, 당신은 dateOnly 객체를 포함한 section 배열 및 분류 Transaction 배열을 가지고있다. tableView을 쉽게 채울 수 있습니다. 결과의

2

미리보기 :

enter image description here

참고 : 나는 스위프트 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? 
}