やりたいこと
日記のようなアプリを作っているのですが、過去の日記を遡りたい時に以下のような「年」をセクションに、「月」をセルの中に並ばせて表示をさせたい。
やったこと・わからないこと
Dictionary(grouping:)を使って日付データを「年」でグループを作ってSectionに対応するところまで出来ました。そこから「月」を自動的に振り分けようとしたのですが、良い方法が思い浮かばず、一個づつSectionとrowを指定して反映させています。
●セクションの数--numberOfRowsInSection
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: return 1 case 1: return 2 case 2: return 1 default: break } return 0 }
●UITableView--cellForRowAt
switch indexPath.section { case 0: let section = self.sections2[0] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) case 1: switch indexPath.row { case 0: let section = self.sections2[1] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) case 1: let section = self.sections2[2] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) default: break } case 2: let section = self.sections2[3] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) default: break }
何か良い方法があればアドバイス・回答、お待ちしております。
全体コードは以下になります。
import UIKit struct Headline { var id : Int var date : Date var title : String var text : String var image : String } private func firstDayOfMonth(date: Date) -> Date { let calendar = Calendar.current let components = calendar.dateComponents([.year], from: date) return calendar.date(from: components)! } private func firstDayOfMonth2(date: Date) -> Date { let calendar = Calendar.current let components = calendar.dateComponents([.year,.month], from: date) return calendar.date(from: components)! } struct YearSection { var month : Date var headlines : [Headline] static func group(headlines : [Headline]) -> [YearSection] { let groups = Dictionary(grouping: headlines) { headline in firstDayOfMonth(date: headline.date) } return groups.map(YearSection.init(month:headlines:)) } } struct MonthSection { var month : Date var headlines : [Headline] static func group(headlines : [Headline]) -> [MonthSection] { let groups = Dictionary(grouping: headlines) { headline in firstDayOfMonth2(date: headline.date) } return groups.map(MonthSection.init(month:headlines:)) } } private func parseDate(_ str : String) -> Date { let dateFormat = DateFormatter() dateFormat.dateFormat = "yyyy-MM-dd" return dateFormat.date(from: str)! } class StoriesTableViewController: UITableViewController { var headlines = [ Headline(id: 1, date: parseDate("2018-05-15"), title: "aaa", text: "aaa", image: "Blueberry"), Headline(id: 2, date: parseDate("2018-02-15"), title: "bbb", text: "bbb", image: "Cantaloupe"), Headline(id: 2, date: parseDate("2018-02-19"), title: "bbb", text: "bbb", image: "Cantaloupe"), Headline(id: 3, date: parseDate("2017-04-05"), title: "ccc", text: "ccc", image: "Apple"), Headline(id: 4, date: parseDate("2020-09-10"), title: "ddd", text: "ddd", image: "Banana"), ] var sections = [YearSection]() var sections2 = [MonthSection]() // MARK: - View Controller lifecycle override func viewDidLoad() { super.viewDidLoad() self.sections = YearSection.group(headlines: self.headlines) self.sections.sort { lhs, rhs in lhs.month < rhs.month } self.sections2 = MonthSection.group(headlines: self.headlines) self.sections2.sort { lhs, rhs in lhs.month < rhs.month } } // MARK: - UITableViewDataSource override func numberOfSections(in tableView: UITableView) -> Int { print(sections) print("sec2+\(sections2)") return self.sections.count } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { let section = self.sections[section] let date = section.month let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy" return dateFormatter.string(from: date) } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: return 1 case 1: return 2 case 2: return 1 default: break } return 0 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) let dateFormatter = DateFormatter() dateFormatter.dateFormat = "YYYY/MM" switch indexPath.section { case 0: let section = self.sections2[0] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) case 1: switch indexPath.row { case 0: let section = self.sections2[1] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) case 1: let section = self.sections2[2] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) default: break } case 2: let section = self.sections2[3] let date = section.month cell.textLabel?.text = dateFormatter.string(from: date) default: break } return cell } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。