["2016/08/26", "2016/08/26" ,"2016/08/26", "2016/08/01", "2016/07/27", "2016/05/26", "2016/05/25", "2015/02/21"]
上記のような形式で日付の入ったarrayがあります。
sectionのheaderに2016/08というように年と月を表示し、rowのLabelに26というように日を表示させたいです。
上記の日付の場合だと
[2016/08]
26
26
26
01
[2016/07]
27
[2016/05]
26
20
[2015/02]
21
のようになります。
上記のarrayからsection用のarrayを2016/08,2016/07,2016/05,2015/02..というように作成することはできました。
sectionだけは表示できていますが、それにともなった日をrowに表示するということができていません。
どなたか、よろしくお願いします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/26 00:39
回答2件
0
ベストアンサー
reduce
を使用してみました、以下のようにすることで抽出、ソートできます、試してみてください。
※ 2つの配列を一緒に作成しているので若干読みにくいかも、、、
ロジック部分のみ
swift
1var dataArray = ["2016/08/26", "2016/08/26", "2016/08/26", "2016/08/01", "2016/07/27", "2016/05/26", "2016/05/25", "2015/02/21"] 2 3var resultArray = dataArray.reduce(([],[])) { (resultTuple, str) -> ([String], [[String]]) in 4 5 var result1 = resultTuple.0 6 var result2 = resultTuple.1 7 8 let yearStr = (str as NSString).substringToIndex(7) 9 let dayStr = (str as NSString).substringFromIndex(8) 10 11 if !result1.contains(yearStr) { 12 result1 += [yearStr] 13 14 let array = [dayStr] 15 result2.append(array) 16 17 } else { 18 result2[result2.count - 1].append(dayStr) 19 } 20 return (result1, result2) 21} 22 23resultArray.0 24//=> ["2016/08", "2016/07", "2016/05", "2015/02"] 25resultArray.1 26//=> [["26", "26", "26", "01"], ["27"], ["26", "25"], ["21"]]
UITableView全体
swift
1import UIKit 2 3class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet weak var tableView: UITableView! 6 var sectionTitleArray: [String] = [] 7 var sectionDataArray: [[String]] = [] 8 9 // Date Array 10 var dataArray = ["2016/08/26", "2016/08/26", "2016/08/26", "2016/08/01", "2016/07/27", "2016/05/26", "2016/05/25", "2015/02/21"] 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 let resultArray = dataArray.reduce(([],[])) { (resultTuple, str) -> ([String], [[String]]) in 16 17 var result1 = resultTuple.0 18 var result2 = resultTuple.1 19 20 let yearStr = (str as NSString).substringToIndex(7) 21 let dayStr = (str as NSString).substringFromIndex(8) 22 23 if !result1.contains(yearStr) { 24 result1 += [yearStr] 25 26 let array = [dayStr] 27 result2.append(array) 28 29 } else { 30 result2[result2.count - 1].append(dayStr) 31 } 32 return (result1, result2) 33 } 34 35 sectionTitleArray = resultArray.0 36 sectionDataArray = resultArray.1 37 38 tableView.tableFooterView = UIView() 39 tableView.estimatedRowHeight = 20 40 tableView.rowHeight = UITableViewAutomaticDimension 41 } 42 43 // MARK: - TableView Delegate & DataSource 44 45 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 46 return sectionTitleArray[section] 47 } 48 49 // Section Count 50 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 51 return sectionDataArray.count 52 } 53 54 // Row Count 55 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 56 return sectionDataArray[section].count 57 } 58 59 // Generate Cell 60 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 61 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 62 cell.textLabel?.text = sectionDataArray[indexPath.section][indexPath.row] 63 return cell 64 } 65 66 // Select Cell 67 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 68 tableView.deselectRowAtIndexPath(indexPath, animated: true) 69 } 70}
投稿2016/08/26 04:11
編集2016/08/26 04:21総合スコア8490
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
目標
最終的にこういう形を目指します。
色々と都合が良かったので年月と日を同じ配列に入れることにしました。
[ ["2016/08", "26", "26", "26", "01"], ["2016/07", "27"], ["2016/05", "26", "25"], ["2016/02", "21"], ]
コード
教えていただいたuniqueを使ってます。
もっとSwiftらしいカッコイイ書き方が出来るかも知れませんが、
ヘッポコなのでこんな感じです。
swift
1//元データ(日付順に並んでいるものとします) 2let array = [ 3 "2016/08/26", 4 "2016/08/26", 5 "2016/08/26", 6 "2016/08/01", 7 "2016/07/27", 8 "2016/05/26", 9 "2016/05/25", 10 "2015/02/21", 11] 12 13//振り分けArray 14var sorted = [Array<String>]() 15print(sorted) 16//=> [] 17 18//重複削除済みの年月Arrayを生成 19let arrayYearMonth = array.map { 20 $0.substringToIndex($0.startIndex.advancedBy(7)) 21}.unique() 22print(arrayYearMonth) 23//=> ["2016/08", "2016/07", "2016/05", "2015/02"] 24 25//振り分けArrayを初期化(先頭に年月を入れておく) 26for yearMonth in arrayYearMonth { 27 sorted.append([yearMonth]) 28} 29print(sorted) 30//=> [["2016/08"], ["2016/07"], ["2016/05"], ["2015/02"]] 31 32//日を振り分け 33for date in array { 34 let yearMonth: String = date.substringToIndex(date.startIndex.advancedBy(7)) 35 let day: String = date.substringFromIndex(date.endIndex.advancedBy(-2)) 36 sorted[arrayYearMonth.indexOf(yearMonth)!].append(day) 37} 38print(sorted) 39//=> [["2016/08", "26", "26", "26", "01"], ["2016/07", "27"], ["2016/05", "26", "25"], ["2015/02", "21"]]
sorted[section][0]でセクションの内容(もしくは、arrayYearMonth[section])、
sorted[section][row+1]でセルの内容を取り出せます。
投稿2016/08/26 00:26
編集2016/08/26 03:01総合スコア16731
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/26 00:32
2016/08/26 00:41
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。