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

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

新規登録して質問してみよう
ただいま回答率
85.49%
iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

2225閲覧

【Swift】UIcollectionViewCellに配置したUIStepperにタッチイベントが止められている?

mk_mkee

総合スコア10

iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/04/26 17:14

現在,私はECアプリを作成しています.
商品リスト画面では,UICollectionViewと自前で用意したCellを用いて,商品をリスト表示したいと考えています.
Cell内にはUIStepperを配置し,各商品をいくつ購入するかをユーザに設定してもらうことを想定しています.
また,商品リスト画面の上部に,現在選択している商品の合計金額を動的に表示したいと考えています.

Swift

1class ItemListViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout { 2 3 @IBOutlet weak var total: UILabel! //合計金額を表示するLabel 4 @IBOutlet weak var collectionView: UICollectionView! 5 6 //商品データ 7 var itemList:[(name:String, price:Int, imgName:String)] = [] 8 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 //-------------------------------------------------- 14 //CustomCellをViewに登録 15 let nibName = UINib(nibName: "ItemCellView", bundle: nil) 16 collectionView.registerNib(nibName, forCellWithReuseIdentifier: "cell") 17 18 //-------------------------------------------------- 19 //商品のデータを用意 20 itemList.append( (name:"いちご", price:100, imgName:"ichigo") ) 21 itemList.append( (name:"ぶどう", price:200, imgName:"ぶどう") ) 22 itemList.append( (name:"さんま", price:300, imgName:"samma") ) 23 itemList.append( (name:"ワイン", price:1000, imgName:"wine") ) 24 itemList.append( (name:"ヤクルト", price:80, imgName:"yakuruto") ) 25 26 27 } 28 29 override func didReceiveMemoryWarning() { 30 super.didReceiveMemoryWarning() 31 // Dispose of any resources that can be recreated. 32 } 33 34 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 35 let cell:ItemCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ItemCell 36 37 //セルにデータをセット 38 cell.title.text = itemList[indexPath.row].name 39 cell.image.image = UIImage(named: "pic/" + itemList[indexPath.row].imgName) 40 cell.price.text = "¥" + String(itemList[indexPath.row].price) 41 42 return cell 43 } 44 45 46 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 47 return 1 48 } 49 50 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 51 return itemList.count; 52 } 53 54 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 55 //Stepperを押した時はイベントが飛んでこない 56 print("たっちすけっちわんたっち!") 57 } 58}

Swift

1class ItemCell: UICollectionViewCell { 2 3 @IBOutlet weak var title: UILabel! //商品名 4 @IBOutlet weak var image: UIImageView! //商品画像 5 @IBOutlet weak var number: UILabel! //注文数 6 @IBOutlet weak var price: UILabel! //価格 7 8 override init(frame: CGRect){ 9 super.init(frame: frame) 10 } 11 required init?(coder aDecoder: NSCoder){ 12 super.init(coder: aDecoder) 13 } 14 15 //商品の個数をステッパーで変更 16 @IBAction func changedStepperValue(sender: UIStepper) { 17 let num = Int(sender.value) 18 //ラベルに表示 19 let gray = UIColor.grayColor() 20 number.backgroundColor = gray.colorWithAlphaComponent(0.6) 21 number.textColor = UIColor.whiteColor() 22 number.text = String(num) 23 24 //0の時は表示しない 25 if num == 0 { 26 number.text = "" 27 number.backgroundColor = UIColor.clearColor() 28 } 29 30 } 31}

そこで,Cell内のStepperが押されたときに,ItemListViewControllerで合計金額のラベル(total)を更新するため,
Cellが選択されたときのイベントを取得しようと,以下のメソッドを実装しました.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

ところが,Cell内のStepper以外の部分をタッチすると,確かにイベントは飛んできているのが確認できたのですが,
Stepperをタッチした場合は,Cell選択時のイベントが飛んで来ませんでした.

Cell内のStepperのイベントをItemListViewController側から取得する方法はありますでしょうか???

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

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

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

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

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

guest

回答1

0

ベストアンサー

didSelectItemAtIndexPathはセルがタッチされたかですので、Stepperの状態が変化したかどうかをチェックするには、StepperのvalueChangedイベントを監視すれば良いです。

各Stepperのtagにセルのインデックスを入れておくなどして、セルを作成する部分で

swift

1cell.stepper.tag = indexPath.row 2cell.stepper.addTarget(self, action: "onStepperChanged:", forControlEvents: UIControlEvents.ValueChanged)

などとしておいて、

swift

1func onStepperChanged(sender: UIStepper) { 2 //sender.tagでどの商品の個数が変化したかを取得して処理 3}

のようなメソッドを用意しておけば良いと思います。
手打ちですので、ビルドが通らなかったら適宜直して下さい。

投稿2016/04/26 21:37

terushu

総合スコア358

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

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

mk_mkee

2016/04/27 02:53

ありがとうございます! 上記の方法で実装できました. cell.stepper.addTarget(self, action: "onStepperChanged:", forControlEvents: UIControlEvents.ValueChanged) の3つ目の引数は「指定したactionをどのイベントの時のactionにするか」を表しているという理解でよろしいでしょうか
terushu

2016/04/27 03:24

3つめの引数についてはその通りです。 お役に立ててなによりでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問