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

回答編集履歴

1

s修正

2016/09/01 23:21

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,13 +1,17 @@
1
- セルのボタン押下アクションをセルで処理するようして、セルを生成するときの`NSIndexPath`から`fileName`を取得すると再生れると思ます
1
+ セルのボタンアクションをセルに結んでください。
2
2
 
3
+ `delegate`を使用して`ViewController`の処理を呼び出しています、`cell`生成時にセルに`index`を渡して`Delegateメソッド`を呼び出す時の引数に更に渡しています。
4
+
3
5
  ```swift
4
6
  import UIKit
7
+ import AVFoundation
5
8
 
6
- class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
9
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {
7
10
 
8
11
  let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"]
9
12
 
10
13
  let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"]
14
+ var audioPlayer = AVAudioPlayer()
11
15
 
12
16
  let imageDescriptions = [
13
17
  "イヌ",
@@ -36,15 +40,37 @@
36
40
 
37
41
  cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row])
38
42
  cell.index = indexPath
43
+ cell.delegate = self
39
44
 
40
45
  return cell
41
46
  }
47
+
48
+ func selectCellButton(index: NSIndexPath) {
49
+ let fileName = "sound\(index.row + 1)"
50
+
51
+ print(fileName)
52
+
53
+ do {
54
+ let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")
55
+ let audioPath = NSURL(fileURLWithPath: filePath!)
56
+ audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath)
57
+ if audioPlayer.prepareToPlay() {
58
+ audioPlayer.play()
59
+ }
60
+ } catch {
61
+ print("Error")
62
+ }
63
+ }
42
64
  }
43
65
 
44
66
  // <CustomTableViewCellのコード>
45
67
  import UIKit
46
- import AVFoundation
47
68
 
69
+ // protocol定義
70
+ protocol CustomTableViewCellDelegate: class {
71
+ func selectCellButton(index: NSIndexPath)
72
+ }
73
+
48
74
  class CustomTableViewCell: UITableViewCell {
49
75
 
50
76
  @IBOutlet weak var myImageView: UIImageView!
@@ -52,41 +78,28 @@
52
78
  @IBOutlet weak var myDescriptionLabel: UILabel!
53
79
  @IBOutlet weak var tapButton: UIButton!
54
80
 
81
+ weak var delegate: CustomTableViewCellDelegate!
55
82
  var index: NSIndexPath!
56
- var audioPlayer = AVAudioPlayer()
57
83
 
58
84
  override func awakeFromNib() {
59
85
  super.awakeFromNib()
60
86
  // Initialization code
61
87
  }
62
88
 
63
- override func setSelected(selected: Bool, animated: Bool) {
89
+ override func setSelected(selected: Bool, animated: Bool) {
64
90
  super.setSelected(selected, animated: animated)
65
91
 
66
- // Configure the view for the selected state
92
+ // Configure the view for the selected state
67
93
  }
68
94
 
69
- func setCell(imageName: String, titleText: String, descriptionText: String) {
95
+ func setCell(imageName: String, titleText: String, descriptionText: String) {
70
- myImageView.image = UIImage(named: imageName)
96
+ myImageView.image = UIImage(named: imageName)
71
- myTitleLabel.text = titleText
97
+ myTitleLabel.text = titleText
72
- myDescriptionLabel.text = descriptionText
98
+ myDescriptionLabel.text = descriptionText
73
99
  }
74
100
 
75
101
  @IBAction func tapButton(sender: AnyObject) {
76
-
77
- let fileName = "sound\(index.row + 1)"
78
-
79
- print(fileName)
80
-
81
- do {
82
- let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")
83
- let audioPath = NSURL(fileURLWithPath: filePath!)
84
- audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath)
85
- audioPlayer.prepareToPlay()
102
+ delegate?.selectCellButton(index)
86
- audioPlayer.play()
87
- } catch {
88
- print("Error")
89
- }
90
103
  }
91
104
  }
92
105
  ```