質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

date

dateは、date型や日付に関する関数や処理についてのタグです

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

Q&A

2回答

2406閲覧

[Swift]昇順表示した開始時刻の配列にセットで終了時刻とタイトルも表示させる方法

niship

総合スコア37

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

date

dateは、date型や日付に関する関数や処理についてのタグです

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

0グッド

0クリップ

投稿2017/05/21 23:21

言葉にするのが難しく、分かりづらい質問になってしまうと思います。
現在、スケジュールアプリを作成しております。
そこで、ユーザーが指定した「タイトル」「開始時刻」「終了時刻」をtableviewcellに表示させたいと思っています。

その際に「開始時刻」のみを昇順にし、「開始時刻」にセットで「終了時刻」と「タイトル」も付随させたいのですが、

開始時刻のみが昇順表示になるため、「タイトル」と「終了時刻」は登録した順番に表示されてしまいます。

SimulatorのScreenShotを添付いたします。
登録した順番
開始時刻:2:00 終了時刻:2:00 タイトル:2
開始時刻:1:00 終了時刻:1:00 タイトル:2

実現したい順番
開始時刻:1:00 終了時刻:1:00 タイトル:2
開始時刻:2:00 終了時刻:2:00 タイトル:1

イメージ説明

下記にcellForRowAt内の現在のコードを記します。
その他に確認が必要な箇所のコードがありましたら、お申し付けください。
ご教示のほど、お願い申しあげます。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell func dateString(date: NSDate) -> String { let dateFormatter = DateFormatter() dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)! as Calendar! dateFormatter.locale = NSLocale.system dateFormatter.timeZone = NSTimeZone.system dateFormatter.dateFormat = "H:mm" let dateString: String = dateFormatter.string(from: date as Date) return dateString } let sortArray = dateArray1.sorted { $0 < $1 } strBox.append(dateString(date: sortArray[indexPath.row] as NSDate)) strBox2.append(dateString(date: dateArray2[indexPath.row] as NSDate)) dateInStr = dateString(date: sortArray[indexPath.row] as NSDate) dateInStr2 = dateString(date: dateArray2[indexPath.row] as NSDate) strCount = dateInStr.characters.count strCount2 = dateInStr2.characters.count if strCount < 5 { timeLabelText1 = " " + strBox[indexPath.row]! + " " } else { timeLabelText1 = strBox[indexPath.row]! + " " } if strCount2 < 5 { timeLabelText2 = " " + strBox2[indexPath.row]! } else { timeLabelText2 = " " + strBox2[indexPath.row]! } let attrText = NSMutableAttributedString(string: timeLabelText1 + ">>>" + timeLabelText2) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(7, 3)) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(0, 5)) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(12, 5)) cell.timeLabel?.attributedText = attrText cell.titleLabel?.text = " " + titleArray[indexPath.row]! // 枠のカラー cell.titleLabel.layer.borderColor = UIColor.darkGray.cgColor // 枠の幅 cell.titleLabel.layer.borderWidth = 1.0 // 枠を角丸にする cell.titleLabel.layer.cornerRadius = 10.0 cell.titleLabel.layer.masksToBounds = true return cell

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2017/05/21 23:49

コードは ``` で囲って下さい。
guest

回答2

0

3つのデータを一纏めにしましょう。
ArrayでもDictionaryでもタプルでもいいです。

タプルを使ったサンプルです。

swift

1let array = [ 2 ("1", "5", "first"), 3 ("8", "9", "fifth"), 4 ("4", "5", "third"), 5 ("6", "8", "fourth"), 6 ("2", "7", "second"), 7] 8 9let sorted = array.sorted {$0.0 < $1.0} 10sorted.forEach { 11 print("start:\($0.0) end:\($0.1) title:\($0.2)") 12}

出力

start:1 end:5 title:first start:2 end:7 title:second start:4 end:5 title:third start:6 end:8 title:fourth start:8 end:9 title:fifth

投稿2017/05/22 01:05

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

解決方法:
各個別データを1つの構造体、クラスなどのオブジェクトとしてデータを保持する

現象理由:
表示する各データのデータ構造に関連性が無いため

色々情報が不足しているので、こちらで読み取った内容で記載します。
過不足あればご指摘ください。また、swift構文は私ほとんど知らないため、
構文ミスなどは適宜読み替えてください。
あとこのメソッドは恐らく呼び出し元がループ処理となっていると想定されるため
当該メソッド内でソートすると、スケジュール作成ごとにソート処理されるため無駄
処理かと。

titleArray:タイトルが保持されている
dateArray1:開始時間が保持されている
dateArray2:終了時間が保持されている

let sortArray = dateArray1.sorted { $0 < $1 }

上記のコードで「開始時間」のソートを行う。行った時点で、dateArray1が昇順で
ソートされる。
だがしかし、この時点においてソート前の各データの関連性が消失する。
例)以下のようにデータがあるとする時

titleArray:["2番目のデータです","1番目のデータです"] dateArray1:["2:00","1:00"] dateArray2:["2:00","1:00"]

dateArray1をソートすると

titleArray:["2番目のデータです","1番目のデータです"] dateArray1:["1:00","2:00"]// 昇順データ dateArray2:["2:00","1:00"]

この時点で、dateArray1の情報とほかのtitleArrayとdateArray2の情報の関連性
がなくなってしまう。なくなった状態で

strBox.append(dateString(date: sortArray[indexPath.row] as NSDate)) strBox2.append(dateString(date: dateArray2[indexPath.row] as NSDate)) cell.titleLabel?.text = " " + titleArray[indexPath.row]!

と行ってしまえば実質的に各々のデータが昇順なったデータとそうではない順番の
データが混在することになってしまう。
よって、各データを1つのデータ構造として利用する
※今回はデータの利用内容などが不明慮なため、構造体として保持

/* 表示データ */ struct ScheduleData{ // スケジュールのコメント var title: String = "" // スケジュールの開始時間 var startDate: date! // スケジュールの終了時間 var endDate: date! init(title : String , startDate : Date , endDate : Date){ self.title = title self.startDate = startDate self.endDate = endDate } } // データ作成 var schedules = [ScheduleData]() // startとendの各DATEには適切な時間を代入してください schedules.append(ScheduleData(title : "1番目のデータです", startDate : Date() , endDate : Date())) // startとendの各DATEには適切な時間を代入してください schedules.append(ScheduleData(title : "2番目のデータです", startDate : Date() , endDate : Date())) // tableViewの呼び出し元 // スケジュールのソート var sortedSchedules = schedules.sorted(by: { $0.startDate>$1.startDate}) let count = sortedSchedules.count for i in 0..<count{ var cell = tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) } // 実装例 // IndexPathではなく、上記呼び出しもとで構造体を渡してもいいと思いますが。 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell func dateString(date: NSDate) -> String { let dateFormatter = DateFormatter() dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)! as Calendar! dateFormatter.locale = NSLocale.system dateFormatter.timeZone = NSTimeZone.system dateFormatter.dateFormat = "H:mm" let dateString: String = dateFormatter.string(from: date as Date) return dateString } // ソートは終わってるのでソートしない // let sortArray = dateArray1.sorted { $0 < $1 } dateInStr = dateString(date: sortedSchedules[indexPath.row].startDate as NSDate) dateInStr2 = dateString(date: sortedSchedules[indexPath.row].endDate as NSDate) strBox.append(dateInStr) strBox2.append(dateInStr2) timeLabelText1 = dateStrFormat(dateInStr) + " " timeLabelText2 = " " + dateStrFormat(dateInStr2) let attrText = NSMutableAttributedString(string: timeLabelText1 + ">>>" + timeLabelText2) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(7, 3)) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(0, 5)) attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(12, 5)) cell.timeLabel?.attributedText = attrText cell.titleLabel?.text = " " + sortedSchedules[indexPath.row].title // 枠のカラー cell.titleLabel.layer.borderColor = UIColor.darkGray.cgColor // 枠の幅 cell.titleLabel.layer.borderWidth = 1.0 // 枠を角丸にする cell.titleLabel.layer.cornerRadius = 10.0 cell.titleLabel.layer.masksToBounds = true return cell } func dateStrFormat(_ dateStr :String)-> String{ var strCount = dateStr.characters.count var PADDING_STRING_COUNT = 5 if strCount < PADDING_STRING_COUNT { return " " + dateStr! } return dateStr! }

投稿2017/05/22 01:04

編集2017/05/22 01:05
kanimaru

総合スコア1013

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

niship

2017/05/22 09:10

ご回答有り難うございます。 // tableViewの呼び出し元 // スケジュールのソート var sortedSchedules = schedules.sorted(by: { $0.startDate>$1.startDate}) let count = sortedSchedules.count for i in 0..<count{ var cell = tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)} こちらはどこに書けばいいのでしょうか?
kanimaru

2017/05/23 07:52

nishipさん nishipさんのコードの全容がわからないので、どこに書くかと言われても困ります。 nishipさんのコードの中で、tableViewメソッドを呼び出しているメソッドがあるのではないでしょうか?tableViewメソッドを呼び出している元のメソッド内で処理することになります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問