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

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

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

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

Swift

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

配列

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

Q&A

1回答

2104閲覧

[Swift]TableViewCellに配列(構造体)のデータが表示されない

niship

総合スコア37

TableView

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

Swift

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

配列

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

0グッド

0クリップ

投稿2017/05/26 02:26

ご覧いただき、有難うございます。
現在スケジュールアプリを作成しております。
ユーザーが指定した「開始時刻(startDate)」「終了時刻(endDate)」「タイトル(title)」を構造体にまとめ、配列として表示したいと思っています。
また、並び順は開始時刻の昇順で表示したいと思っています。

「開始時刻(startDate)」「終了時刻(endDate)」「タイトル(title)」は、DetailsViewController.swiftにてテキストフィールドに入力された値をschedules(構造体ScheduleDataのインスタンス)に追加します。その後、schedulesをNSDictionaryに変換しUserDefaultsに保存しています。
ViewController.swiftのTableViewCellにはtimeLabel(開始時刻+終了時刻を表示するラベル(UILabel))とtitleLabel(タイトルを表示するラベル(UILabel))を置いています。各ラベルはTableViewCell.swiftという別ファイルに接続して、ViewControllerで呼び出しています。
各ラベルにUserDefaultsにて保存したschedulesから取り出した「開始時刻(Date)」「終了時刻(Date)」「タイトル(String)」を表示したいと思っています。

上記を踏まえてコードを書いたのですが、セルに何も表示されません。
以下に関係性があるであろうコードを記述致します。
その他に確認が必要な箇所のコードが有りましたら、ご指摘くださいませ。
また、質問内容でわかりにくい点がありましたら、そちらもご指摘くださいませ。

// SchedulesData.swift import Foundation /* 表示データ */ 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 } } extension UserDefaults { var logDataArray:[ScheduleData]{ set(datas){ // Swiftのオブジェクトを、NSObjectなオブジェクトに変換する let newDatas:[NSDictionary] = datas.map{ ["title":$0.title, "startdate":$0.startDate, "endDate":$0.endDate] as NSDictionary } // NSObjectなオブジェクトのみになったから、setObjectできる self.set(newDatas,forKey:"schedules") } get{ // NSDictionaryの配列として、データを取得 let datas = self.object(forKey: "schedules") as? [NSDictionary] ?? [] // 保存されたデータから復元出来無い場合もあり得るので、 // mapではなくreduceを使う let array = datas.reduce([]){ (ary, d:NSDictionary) -> [ScheduleData] in // dateやmessageがnilでないなら、MyLogDataを作って足し込む if let title = d["title"] as? String, let endDate = d["endDate"] as? Date, let startDate = d["startDate"] as? Date { return ary + [ScheduleData(title: title, startDate: startDate, endDate: endDate)] }else{ return ary } } return array } } }
// DetailsViewController.swift func saveData() { dataOut() if dateTextField.text! != nil { if dateTextField2.text! != nil { if titleTextField.text! != nil { dateFromString = myDateFormatter.date(from: dateTextField.text!)! dateFromString2 = myDateFormatter.date(from: dateTextField2.text!)! dateArrayFromString.append(dateFromString) dateArrayFromString.append(dateFromString2) schedules.append(ScheduleData(title:titleTextField.text!, startDate: dateFromString, endDate: dateFromString2)) detailArray.append(detailTextView.text!) placeArray.append(placeTextField.text!) urlArray.append(urlTextField.text!) alarmDateArray.append(alarmTimeTextField.text!) UserDefaults.standard.logDataArray = schedules UserDefaults.standard.set(detailArray, forKey: "detailArray") UserDefaults.standard.set(placeArray, forKey: "placeArray") UserDefaults.standard.set(urlArray, forKey: "urlArray") UserDefaults.standard.set(alarmDateArray, forKey: "alarmDateArray") } } } else { warningAlertController() } }
// ViewController.swift func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { return false } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return schedules.count } 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 } strBox.append(dateString(date: sortedSchedules[indexPath.row].startDate as NSDate)) strBox2.append(dateString(date: sortedSchedules[indexPath.row].endDate as NSDate)) dateInStr = dateString(date: sortedSchedules[indexPath.row].startDate as NSDate) dateInStr2 = dateString(date: sortedSchedules[indexPath.row].endDate 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 = " " + 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 }
//ViewWillApperに呼び出しています。 func dataOut() { if UserDefaults.standard.logDataArray != nil { schedules = UserDefaults.standard.logDataArray } if UserDefaults.standard.array(forKey: "detailArray") != nil { detailArray = UserDefaults.standard.array(forKey: "detailArray") as! [String] } if UserDefaults.standard.array(forKey: "placeArray") != nil { placeArray = UserDefaults.standard.array(forKey: "placeArray") as! [String] } if UserDefaults.standard.array(forKey: "urlArray") != nil { urlArray = UserDefaults.standard.array(forKey: "urlArray") as! [String] } if UserDefaults.standard.array(forKey: "alarmDateArray") != nil { alarmDateArray = UserDefaults.standard.array(forKey: "alarmDateArray") as! [String] } }

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

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

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

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

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

fuzzball

2017/05/26 02:55 編集

「セルに何も表示されません」ということは、セル自体は表示されているということでしょうか?
niship

2017/05/26 02:57

今確認してみると、セルが表示されていませんでした。 一旦、CellForRowAt内の 記述をlet cell = と return cell 以外、削除し numberOfRowsInectionの戻り値をreturn 1 変えて検証してみるとセルが表示されました。
fuzzball

2017/05/26 03:50 編集

tableView(_:numberOfRowsInSection:)の中に print(schedules.count) を追加してschedulesの中身があるかどうか確認して下さい。
niship

2017/05/26 03:54

print(schedules.count)を記述すると、デバックエリアに 0 0 0 0 と表示されました。
guest

回答1

0

dataOut()をviewDidLoad()で呼び出して下さい。

投稿2017/05/26 03:59

fuzzball

総合スコア16731

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

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

niship

2017/05/26 04:07

ご回答有難うございます。 ご教示頂いた通り、訂正致しましたが、セルは表示されず、デバックエリアに 0000 と表示されます。
fuzzball

2017/05/26 04:15

dataOut()の schedules = UserDefaults.standard.logDataArray の下に、 print(schedules) を追加してschedulesを取得できているかどうか確認して下さい。
niship

2017/05/26 04:23

確認したところ、 [] 0 0 0 0 と表示されました。
fuzzball

2017/05/26 04:31 編集

前の質問で、schedulesを保存/取得出来ているなら別の質問にして下さいと書きましたが、取得できてないですよね?
niship

2017/05/26 04:37

前の質問ではコンパイルエラーが消え、シミュレータでクラッシュすることもなくなったので、schedulesは保存できているけど、何らかの理由で表示されていないと勘違いしておりました。申し訳有りません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問