teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

誤字修正

2020/02/21 12:17

投稿

duck015
duck015

スコア29

title CHANGED
File without changes
body CHANGED
@@ -149,7 +149,6 @@
149
149
  @IBOutlet weak var newArrivalQuestionListTableView: UITableView!
150
150
 
151
151
  var presenter: QuestionListViewPresenter!
152
- var itemInfo: IndicatorInfo = "新着"
153
152
 
154
153
  override func viewDidLoad() {
155
154
  super.viewDidLoad()

4

文字修正

2020/02/21 12:17

投稿

duck015
duck015

スコア29

title CHANGED
File without changes
body CHANGED
@@ -133,46 +133,7 @@
133
133
  }
134
134
  }
135
135
  ```
136
- ```QuestionListViewPresenter
137
- class QuestionListViewPresenter {
138
136
 
139
- private weak var view: ListViewInterface!
140
-
141
- var questionModel: QuestionModel!
142
- var likeModel = LikeModel()
143
-
144
- var numberOfQuestions: Int {
145
- return questionModel?.questions.count ?? 10
146
- }
147
-
148
- init(view: ListViewInterface) {
149
- self.view = view
150
- self.questionModel = QuestionModel()
151
- self.questionModel.addObserver(self, selector: #selector(self.updated))
152
- }
153
-
154
- @objc func updateQuestions() {
155
- questionModel.fetchNewArivalQuestions()
156
- print("presenter")
157
- }
158
-
159
- func entity(at indexPath: IndexPath) -> QuestionEntity {
160
- return questionModel.questions[indexPath.row]
161
- }
162
-
163
- func didSelectRow(at indexPath: IndexPath) {
164
- view?.navigateDetail(entity: questionModel.questions[indexPath.row])
165
- }
166
-
167
- @objc func updated() {
168
- view?.reloadData()
169
- }
170
-
171
- func likeButtonTapped(at indexPath: IndexPath) {
172
- likeModel.updateLikesData(id: questionModel.questions[indexPath.row].id)
173
- }
174
- }
175
- ```
176
137
  ```NewArrivalQuestionListViewController
177
138
  protocol ListViewInterface: class {
178
139
  func reloadData()
@@ -238,31 +199,5 @@
238
199
  let indexPath = newArrivalQuestionListTableView.indexPathForRow(at: point)!
239
200
  presenter.likeButtonTapped(at: indexPath)
240
201
  }
241
-
242
202
  }
243
-
244
- extension NewArrivalQuestionListViewController: UITableViewDelegate, UITableViewDataSource {
245
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
246
- return presenter.numberOfQuestions
247
- }
248
-
249
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
250
- let cell = newArrivalQuestionListTableView.dequeueReusableCell(withIdentifier: "QuestionListTableViewCell", for: indexPath) as! QuestionListTableViewCell
251
- cell.setQuestion(entity: presenter.entity(at: indexPath))
252
- cell.likeButton.addTarget(self, action: #selector(likeButtonTapped(_:forEvent:)), for: .touchUpInside)
253
- return cell
254
- }
255
-
256
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
257
- newArrivalQuestionListTableView.deselectRow(at: indexPath, animated: true)
258
- presenter.didSelectRow(at: indexPath)
259
- }
260
- }
261
-
262
- extension NewArrivalQuestionListViewController: IndicatorInfoProvider {
263
-
264
- func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
265
- return itemInfo
266
- }
267
- }
268
203
  ```

3

誤字修正

2020/02/21 12:15

投稿

duck015
duck015

スコア29

title CHANGED
File without changes
body CHANGED
@@ -64,7 +64,7 @@
64
64
  }
65
65
  }
66
66
  ```
67
- ```
67
+ ```QuestionModel
68
68
  class QuestionModel {
69
69
 
70
70
  var questions: [QuestionEntity] = []

2

誤字修正

2020/02/11 07:54

投稿

duck015
duck015

スコア29

title CHANGED
File without changes
body CHANGED
@@ -64,6 +64,49 @@
64
64
  }
65
65
  }
66
66
  ```
67
+ ```
68
+ class QuestionModel {
69
+
70
+ var questions: [QuestionEntity] = []
71
+ var notificationName: Notification.Name {
72
+ return Notification.Name(rawValue: "questions")
73
+ }
74
+
75
+ func fetchNewArivalQuestions() {
76
+ print("fetch")
77
+ Alamofire.request("https://teratail.com/api/v1/questions").responseJSON { response in
78
+ self.questions = []
79
+ guard let object = response.result.value else { return }
80
+ let json = JSON(object)
81
+ json["questions"].forEach { (_, json) in
82
+ let id = json["id"].intValue
83
+ let title = json["title"].stringValue
84
+ let tags = json["tags"].arrayObject
85
+ let displayName = json["user"]["display_name"].stringValue
86
+ let photo = json["user"]["photo"].stringValue
87
+ let created = json["created"].stringValue
88
+ let isAccepted = json["is_accepted"].boolValue
89
+ self.questions.append(QuestionEntity(id: id, title: title, tags: tags as! [String], displayName: displayName, photo: photo, created: created, isAccepted: isAccepted))
90
+ }
91
+ self.notify()
92
+ print("通知")
93
+ }
94
+ }
95
+
96
+ func addObserver(_ observer: Any, selector: Selector) {
97
+ NotificationCenter.default.addObserver(observer, selector: selector, name: notificationName, object: nil)
98
+ }
99
+
100
+ func removeObserver(_ observer: Any) {
101
+ NotificationCenter.default.removeObserver(observer)
102
+ }
103
+
104
+ func notify() {
105
+ NotificationCenter.default.post(name: notificationName, object: nil)
106
+ }
107
+
108
+ }
109
+ ```
67
110
 
68
111
  ```QuestionListTableViewCell
69
112
  class QuestionListTableViewCell: UITableViewCell {

1

誤字修正

2020/02/11 07:52

投稿

duck015
duck015

スコア29

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,7 @@
8
8
  ### 発生している問題・エラーメッセージ
9
9
  QuestionEntityのイニシャライザでFirestoreのデータに質問のIDがあるかどうかを判別し、IDがある(`isLiked == true`)場合はQuestionListTableViewCellにてボタンの表示を変化させています。
10
10
 
11
- しかし、`viewDidLoad`、`viewWillAppear`、UIRefershControl実行時にはデフォルトの状態で表示されてしまいます。
11
+ しかし、`viewDidLoad`、`viewWillAppear`、`UIRefershControl`実行時にはデフォルトの状態で表示されてしまいます。
12
12
  つまり、`func updateQuestions()`実行時に問題が発生します。
13
13
  ただ、その後、一度スクロールしてセルを再表示させると、意図した通りにボタンの表示が変更されます。
14
14