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

質問編集履歴

4

改善しました

2021/03/07 13:45

投稿

kamiyama-jota
kamiyama-jota

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,24 +1,27 @@
1
1
  ``````ここに言語を入力
2
+ ```
2
- ```// ViewController.swift
3
+ // ViewController.swift
3
4
  // MyOkashi
4
5
  //
5
6
  // Created by kamiyamajota on 2021/03/03.
6
7
  //
7
8
 
8
9
  import UIKit
10
+ import SafariServices
9
11
 
10
- class ViewController: UIViewController , UISearchBarDelegate,UITableViewDataSource {
12
+ class ViewController: UIViewController,UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,SFSafariViewControllerDelegate {
11
13
 
12
- override func viewDidLoad() {
14
+ override func viewDidLoad() {
13
15
  super.viewDidLoad()
14
16
  // Do any additional setup after loading the view.
15
- // Search Barのdelegate通知を設定
17
+ // Search Ba//rのdelegate通知を設定
16
18
  searchText.delegate = self
17
19
  // 入力のヒントとなる、プレースホルダーを設定
18
20
  searchText.placeholder = "お菓子の名前を入力してください"
19
- // Table ViewのdataSOurceを設定
21
+ // Table ViewのdataSourceを設定
20
22
  tableView.dataSource = self
21
-
23
+ //Table Viewのdelegateを設定
24
+ tableView.delegate = self
22
25
  }
23
26
 
24
27
 
@@ -108,6 +111,7 @@
108
111
  }
109
112
  // Table Viewを更新する
110
113
  self.tableView.reloadData()
114
+
111
115
  if let okashidbg = self.okashiList.first {
112
116
  print ("-----------------")
113
117
  print("okashiList[0] = (okashidbg)")
@@ -127,14 +131,13 @@
127
131
  // お菓子リストの総数
128
132
  return okashiList.count
129
133
  }
130
-
131
134
  // Cellの値を返すdatasourceメゾット。必ず記?する必要があります
132
- func tableview(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
135
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
133
136
  // 今回表示を行う、Cellオブジェクト(1行)を取得する
134
137
  let cell = tableView.dequeueReusableCell(withIdentifier: "okashiCell", for: indexPath)
135
138
  // お菓子のタイトル設定
136
139
  cell.textLabel?.text = okashiList[indexPath.row].name
137
- // お菓子の画像検索を取得
140
+ // お菓子の画像検索を取得
138
141
  if let imageData = try? Data(contentsOf: okashiList[indexPath.row].image) {
139
142
  // 正常に取得出来た場合は、UIImageで画像オブジェストを生成して、Cellにお菓子画像を設定
140
143
  cell.imageView?.image = UIImage(data: imageData)
@@ -142,10 +145,35 @@
142
145
  }
143
146
  // 設定済みのCellオブジェクトを画像に反映
144
147
  return cell
148
+ }
145
149
 
150
+ // Cellが選択された際に呼び出されるdelegateメソッド
151
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
152
+ // ハイライト解除
153
+ tableView.deselectRow(at: indexPath, animated: true)
154
+
155
+ // SFSafaruViewを開く
156
+ let safariViewController = SFSafariViewController(url: okashiList[indexPath.row].link)
157
+
158
+ //delegateの通知先を自分自身
159
+ safariViewController.delegate = self
160
+
161
+ //SafariViewが開かれる
162
+ present(safariViewController,animated: true,completion: nil)
146
163
  }
164
+
165
+ // SafariViewが閉じられたときに呼ばれるdelegateメソッド
166
+ func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
167
+
168
+ // SafariViewを閉じる
169
+ dismiss(animated: true, completion: nil)
170
+ }
147
171
  }
148
172
 
173
+
174
+
175
+
176
+
149
177
  ```
150
178
  ```エラーコード
151
179
  Type 'ViewController' does not conform to protocol 'UITableViewDataSource'

3

改善しました

2021/03/07 13:45

投稿

kamiyama-jota
kamiyama-jota

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
- ![イメージ説明](466bb184a211d67fb60a5dca7b0be5c2.png)![![イメージ説明](bee6f8ac7a2db710fffb2fbf149fa0f5.png)](4df38266afa79b4e7d0ecf214224bdc1.png)![Fixを押したらバツが2つになります。(9fbe3fcb75ee29a91315a689bcfd21fc.png)```//
2
- // ViewController.swift
1
+ ``````ここに言語を入力
2
+ ```// ViewController.swift
3
3
  // MyOkashi
4
4
  //
5
5
  // Created by kamiyamajota on 2021/03/03.
@@ -146,4 +146,7 @@
146
146
  }
147
147
  }
148
148
 
149
- ```
149
+ ```
150
+ ```エラーコード
151
+ Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
152
+ ```![イメージ説明]![イメージ説明](d41cf6e23ff22e0d78b74bedc1adea08.png)

2

書籍の改善

2021/03/07 13:41

投稿

kamiyama-jota
kamiyama-jota

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- ![![イメージ説明](bee6f8ac7a2db710fffb2fbf149fa0f5.png)](4df38266afa79b4e7d0ecf214224bdc1.png)![Fixを押したらバツが2つになります。(9fbe3fcb75ee29a91315a689bcfd21fc.png)```//
1
+ ![イメージ説明](466bb184a211d67fb60a5dca7b0be5c2.png)![![イメージ説明](bee6f8ac7a2db710fffb2fbf149fa0f5.png)](4df38266afa79b4e7d0ecf214224bdc1.png)![Fixを押したらバツが2つになります。(9fbe3fcb75ee29a91315a689bcfd21fc.png)```//
2
2
  // ViewController.swift
3
3
  // MyOkashi
4
4
  //

1

改善しました

2021/03/06 14:49

投稿

kamiyama-jota
kamiyama-jota

スコア2

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- ![イメージ説明](9fbe3fcb75ee29a91315a689bcfd21fc.png)```//
1
+ ![![イメージ説明](bee6f8ac7a2db710fffb2fbf149fa0f5.png)](4df38266afa79b4e7d0ecf214224bdc1.png)![Fixを押したらバツが2つになります。(9fbe3fcb75ee29a91315a689bcfd21fc.png)```//
2
2
  // ViewController.swift
3
3
  // MyOkashi
4
4
  //