swift4.1
swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 4 5 @IBOutlet weak var mytableView: UITableView! 6 var add = UILabel() 7 var item = [String]() 8 9 10 //+ボタン 11 @IBAction func addlabel(_ sender: Any) { 12 alert() 13 } 14 15 //ラベルについて 16 func label(){ 17 add = UILabel(frame: CGRect(x: 130, y:250, width: 100, height:20)) 18 //ラベルの大きさ、座標指定 19 //add.text = "labelです" 20 //文字を変更 21 22 add.backgroundColor = UIColor.lightGray 23 24 add.font = UIFont.systemFont(ofSize: 30) 25 //文字の大きさ 26 27 add.textColor = UIColor.black 28 //文字カラー 29 30 add.sizeToFit() 31 //文字数にあわせてlabelの大きさを変更(サイズが文字にフィットする) 32 33 self.view.addSubview(add) 34 //実際にviewに見える形でlabelが出現する 35 } 36 37 func alert(){ 38 // テキストフィールド付きアラート表示 39 40 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 41 42 // OKボタンの設定 43 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 44 (action:UIAlertAction!) -> Void in 45 46 // OKを押した時入力されていたテキストを表示 47 if let textFields = alert.textFields { 48 49 // アラートに含まれるすべてのテキストフィールドを調べる 50 for textField in textFields { 51 self.label() 52 self.item.insert(textField.text!, at: 0) 53 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 54 self.add.text = textField.text! 55 self.add.sizeToFit() 56 print(textField.text!) 57 self.Gesture() 58 self.doubleclic() 59 } 60 } 61 }) 62 alert.addAction(okAction) 63 64 // キャンセルボタンの設定 65 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 66 alert.addAction(cancelAction) 67 68 // テキストフィールドを追加 69 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 70 textField.placeholder = "テキスト" 71 }) 72 73 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 74 75 // アラートを画面に表示 76 self.present(alert, animated: true, completion: nil) 77 78 } 79 80 func deletealert() { 81 let alert = UIAlertController(title:"you realy want to delete?", message: "メッセージ", preferredStyle: .alert) 82 83 let okAction = UIAlertAction(title: "YES", style: .default, handler: { 84 (action:UIAlertAction!) -> Void in 85 86 //labelの削除について 87 self.add.tag = 1 88 89 self.view.subviews.forEach { 90 if $0.tag == 1{ 91 $0.removeFromSuperview() 92 93 } 94 } 95 }) 96 alert.addAction(okAction) 97 98 // キャンセルボタンの設定 99 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 100 alert.addAction(cancelAction) 101 102 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 103 // アラートを画面に表示 104 self.present(alert, animated: true, completion: nil) 105 106 } 107 108 109 @objc func longpress(sender: UILongPressGestureRecognizer){ 110 111 // 長押し開始〜 112 if(sender.state == UIGestureRecognizerState.began) 113 { 114 115 } else if (sender.state == UIGestureRecognizerState.ended) 116 { 117 //labelの削除について 118 self.add.tag = 1 119 120 self.view.subviews.forEach { 121 if $0.tag == 1{ 122 $0.removeFromSuperview() 123 124 } 125 } 126 alert() 127 print("ロングタップされたよ。") 128 } 129 130 } 131 132 func Gesture() { 133 134 // UILongPressGestureRecognizerインスタンス作成 135 let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(sender:))) 136 // 時間(デフォルト0.5秒) 137 longPressGesture.minimumPressDuration = 0.5 138 self.add.isUserInteractionEnabled = true 139 self.add.addGestureRecognizer(longPressGesture) 140 141 142 143 } 144 145 func doubleclic(){ 146 // ダブルタップ 147 let doubeltapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.doubletap(sender:))) 148 doubeltapGesture.numberOfTapsRequired = 2 149 self.add.isUserInteractionEnabled = true 150 self.add.addGestureRecognizer(doubeltapGesture) 151 } 152 153 @objc func doubletap(sender: UITapGestureRecognizer){ 154 if(sender.state == UIGestureRecognizerState.began) 155 { 156 }else if(sender.state == UIGestureRecognizerState.ended) 157 { 158 deletealert() 159 print("tapされたよ") 160 } 161 162 } 163 164 165 166 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 167 return item.count 168 } 169 170 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 171 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 172 //cell.textLabel?.text = add.text 173 let todoLabel = item[indexPath.row] 174 cell.textLabel?.text = todoLabel 175 return cell 176 } 177 178 179 override func viewDidLoad() { 180 super.viewDidLoad() 181 mytableView.dataSource = self 182 mytableView.delegate = self 183 } 184 185 186 187 override func didReceiveMemoryWarning() { 188 super.didReceiveMemoryWarning() 189 // Dispose of any resources that can be recreated. 190 } 191 192 193} 194 195
やりたいこと
tabelviewのcell内にlabelを挿入したい
困っていること
labelをコードから作っているためcell内に挿入する方法が分からない
エラーは起きないがcellの中に挿入できずにいる。コンソールには入力した文字は表示されるので下記のコードが良くないのかなと推測できるがどうすればいいの分からずいに困っています。
//ラベルについて
func label(){ add = UILabel (frame: CGRect()) ... ... }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/02 18:15
退会済みユーザー
2018/04/03 04:43
2018/04/03 04:50
退会済みユーザー
2018/04/03 05:44
2018/04/03 06:58
退会済みユーザー
2018/04/05 02:24