現在,私は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側から取得する方法はありますでしょうか???
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/27 02:53
2016/04/27 03:24