回答編集履歴

1

s修正

2016/09/01 23:21

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -1,4 +1,8 @@
1
+ セルのボタンアクションをセルに結んでください。
2
+
3
+
4
+
1
- セルボタン押下アクションをセルで処理するようにして、セルを生成するときの`NSIndexPath`から`fileName`を取得ると再生されると思います。
5
+ `delegate`を使用して`ViewController`の処理を呼び出しています`cell`生成時にセル`index`を渡して`Delegateメソッド`を呼び出時の引数に更に渡しています。
2
6
 
3
7
 
4
8
 
@@ -6,9 +10,11 @@
6
10
 
7
11
  import UIKit
8
12
 
9
-
13
+ import AVFoundation
10
-
14
+
15
+
16
+
11
- class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
17
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {
12
18
 
13
19
 
14
20
 
@@ -18,6 +24,8 @@
18
24
 
19
25
  let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"]
20
26
 
27
+ var audioPlayer = AVAudioPlayer()
28
+
21
29
 
22
30
 
23
31
  let imageDescriptions = [
@@ -74,12 +82,48 @@
74
82
 
75
83
  cell.index = indexPath
76
84
 
85
+ cell.delegate = self
86
+
77
87
 
78
88
 
79
89
  return cell
80
90
 
81
91
  }
82
92
 
93
+
94
+
95
+ func selectCellButton(index: NSIndexPath) {
96
+
97
+ let fileName = "sound\(index.row + 1)"
98
+
99
+
100
+
101
+ print(fileName)
102
+
103
+
104
+
105
+ do {
106
+
107
+ let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")
108
+
109
+ let audioPath = NSURL(fileURLWithPath: filePath!)
110
+
111
+ audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath)
112
+
113
+ if audioPlayer.prepareToPlay() {
114
+
115
+ audioPlayer.play()
116
+
117
+ }
118
+
119
+ } catch {
120
+
121
+ print("Error")
122
+
123
+ }
124
+
125
+ }
126
+
83
127
  }
84
128
 
85
129
 
@@ -88,7 +132,15 @@
88
132
 
89
133
  import UIKit
90
134
 
135
+
136
+
91
- import AVFoundation
137
+ // protocol定義
138
+
139
+ protocol CustomTableViewCellDelegate: class {
140
+
141
+ func selectCellButton(index: NSIndexPath)
142
+
143
+ }
92
144
 
93
145
 
94
146
 
@@ -106,10 +158,10 @@
106
158
 
107
159
 
108
160
 
161
+ weak var delegate: CustomTableViewCellDelegate!
162
+
109
163
  var index: NSIndexPath!
110
164
 
111
- var audioPlayer = AVAudioPlayer()
112
-
113
165
 
114
166
 
115
167
  override func awakeFromNib() {
@@ -122,25 +174,25 @@
122
174
 
123
175
 
124
176
 
125
- override func setSelected(selected: Bool, animated: Bool) {
177
+ override func setSelected(selected: Bool, animated: Bool) {
126
178
 
127
179
  super.setSelected(selected, animated: animated)
128
180
 
129
181
 
130
182
 
131
- // Configure the view for the selected state
183
+ // Configure the view for the selected state
132
-
184
+
133
- }
185
+ }
134
-
135
-
136
-
186
+
187
+
188
+
137
- func setCell(imageName: String, titleText: String, descriptionText: String) {
189
+ func setCell(imageName: String, titleText: String, descriptionText: String) {
138
-
190
+
139
- myImageView.image = UIImage(named: imageName)
191
+ myImageView.image = UIImage(named: imageName)
140
-
192
+
141
- myTitleLabel.text = titleText
193
+ myTitleLabel.text = titleText
142
-
194
+
143
- myDescriptionLabel.text = descriptionText
195
+ myDescriptionLabel.text = descriptionText
144
196
 
145
197
  }
146
198
 
@@ -148,33 +200,7 @@
148
200
 
149
201
  @IBAction func tapButton(sender: AnyObject) {
150
202
 
151
-
152
-
153
- let fileName = "sound\(index.row + 1)"
154
-
155
-
156
-
157
- print(fileName)
158
-
159
-
160
-
161
- do {
162
-
163
- let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")
164
-
165
- let audioPath = NSURL(fileURLWithPath: filePath!)
166
-
167
- audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath)
168
-
169
- audioPlayer.prepareToPlay()
203
+ delegate?.selectCellButton(index)
170
-
171
- audioPlayer.play()
172
-
173
- } catch {
174
-
175
- print("Error")
176
-
177
- }
178
204
 
179
205
  }
180
206