swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 4 5 @IBOutlet weak var mytableView: UITableView! 6 7 //構造体 8 var titles: [String] = [] // title用 9 var products: [Cell] = []//detail 10 11 12 class namelabel { 13 var labels: [UILabel] = [] 14 } 15 16 17 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 18 // 全データの合計 19 return titles.count + products.count 20 } 21 22 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 23 let cell = UITableViewCell() 24 let index = Int(floor(Double(indexPath.row / 2))) 25 //セルタップ時のハイライトについて 26 cell.selectionStyle = .none 27 28 // title行 29 if indexPath.row % 2 == 0 { 30 31 //ここ再確認 32 //破線について 33 let path = UIBezierPath() 34 path.setLineDash([6.0, 2.0], count: 2, phase: 0.0) 35 36 guard titles.count > 0 else { 37 return cell 38 } 39 40 41 let title = titles[index] 42 cell.textLabel?.text = title 43 //センター寄せ 44 cell.textLabel!.textAlignment = NSTextAlignment.center 45 cell.backgroundColor = UIColor(red: 0.0, green: 0.8, blue: 1.0, alpha: 0.3) 46 47 // detail行 48 } else { 49 cell.backgroundColor = UIColor.lightGray 50 let item = products[index] 51 52 guard item.detail.count > 0 else { 53 return cell 54 } 55 let hoge = namelabel() 56 //labelについて 57 for i in item.detail { 58 let label = UILabel() 59 label.backgroundColor = UIColor(red: 0.0, green: 0.8, blue: 0.9, alpha: 0.2) 60 label.text = i 61 62 63 // textLabel という名前の変数に格納された UILabel にフォントサイズの自動調整を設定します。 64 label.adjustsFontSizeToFitWidth = true 65 label.minimumScaleFactor = 10.0 66 //丸みに対して 67 label.layer.cornerRadius = 5 68 label.layer.masksToBounds = true 69 cell.contentView.addSubview(label) 70 hoge.labels.append(label) 71 } 72 73 var preLabel: UILabel? = nil 74 for l in hoge.labels { 75 76 l.translatesAutoresizingMaskIntoConstraints = false 77 if (preLabel == nil) { 78 l.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 12).isActive = true 79 } else { 80 l.leftAnchor.constraint(equalTo: preLabel!.rightAnchor, constant: 20).isActive = true 81 } 82 cell.contentView.heightAnchor.constraint(equalTo: l.heightAnchor, multiplier: 1).isActive = true 83 preLabel = l 84 } 85 86 } 87 88 return cell 89 }
困っている箇所
swift
1func deletealert() { 2 let hoge = namelabel() 3 let alert = UIAlertController(title:"you realy want to delete?", message: "メッセージ", preferredStyle: .alert) 4 5 let okAction = UIAlertAction(title: "YES", style: .default, handler: { 6 (action:UIAlertAction!) -> Void in 7 8 //labelの削除について 9 hoge.labels.tag = 1 10 11 self.view.subviews.forEach { 12 if $0.tag == 1{ 13 $0.removeFromSuperview() 14 15 } 16 } 17 }) 18 alert.addAction(okAction) 19 20 // キャンセルボタンの設定 21 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 22 alert.addAction(cancelAction) 23 24 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 25 // アラートを画面に表示 26 self.present(alert, animated: true, completion: nil) 27 28 } 29 30 //長押しやダブルクリックについて 31 @objc func longpress(sender: UILongPressGestureRecognizer){ 32 let hoge = namelabel() 33 // 長押し開始〜 34 if(sender.state == UIGestureRecognizerState.began) 35 { 36 37 } else if (sender.state == UIGestureRecognizerState.ended) 38 { 39 //labelの削除について 40 hoge.labels.tag = 1 41 42 self.view.subviews.forEach { 43 if $0.tag == 1{ 44 $0.removeFromSuperview() 45 46 } 47 } 48 //alert() 49 print("ロングタップされたよ。") 50 } 51 52 } 53 54 @objc func doubletap(sender: UITapGestureRecognizer){ 55 if(sender.state == UIGestureRecognizerState.began) 56 { 57 }else if(sender.state == UIGestureRecognizerState.ended) 58 { 59 deletealert() 60 print("tapされたよ") 61 } 62 63 } 64 65 func Gesture() { 66 67 let hoge = namelabel() 68 // UILongPressGestureRecognizerインスタンス作成 69 let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(sender:))) 70 71 // 時間(デフォルト0.5秒) 72 longPressGesture.minimumPressDuration = 0.5 73 hoge.labels[0].isUserInteractionEnabled = true 74 hoge.labels[0].addGestureRecognizer(longPressGesture) 75 76 77 78 } 79 80 func doubleclic(){ 81 let hoge = namelabel() 82 // ダブルタップ 83 let doubeltapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.doubletap(sender:))) 84 doubeltapGesture.numberOfTapsRequired = 2 85 hoge.labels[0].isUserInteractionEnabled = true 86 hoge.labels[0].addGestureRecognizer(doubeltapGesture) 87 }
実現したい事・困っている事
products.isUserInteractionEnabled = true
products.addGestureRecognizer(doubeltapGesture)
Value of type '[Cell]' has no member 'isUserInteractionEnabled'
Value of type '[Cell]' has no member 'addGestureRecognizer'
上記のコードでこの様なエラーが出てしまいました。実現したい事としてはセルの中のラベルをダブルタップや長押しをできる様にしたいです。
しかし、コーディングの際にcellForRowAT内にラベルのインスタンスを作ったのでどう結びつければいいのか分からなくて困っています。
現在困っている事
classを作ることにより上記のエラーは取れましたがそれに伴い、以下の場所でエラーが出来ました。
エラーの意味は理解できるのですがその対処法が分からなくて困っています。
hoge.labels.tag = 1 //Value of type '[UILabel]' has no member 'tag'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/23 11:11 編集
2018/05/23 13:48 編集
退会済みユーザー
2018/05/23 14:48
2018/05/23 15:23
退会済みユーザー
2018/05/24 13:48
2018/05/24 23:25 編集
2018/05/25 11:14
退会済みユーザー
2018/05/25 11:50
2018/05/25 12:16 編集