swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 4 5 @IBOutlet weak var mytableView: UITableView! 6 var item = [String]() 7 8 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 9 return item.count 10 } 11 12 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 13 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 14 let todoLabel = item[indexPath.row] 15 cell.textLabel?.text = todoLabel 16 17 //ボタンについて 18 let button = UIButton() 19 button.backgroundColor = UIColor.blue 20 button.setTitle(" 追 加 ", for: .normal) 21 cell.contentView.addSubview(button) 22 23 button.translatesAutoresizingMaskIntoConstraints = false 24 cell.contentView.rightAnchor.constraint(equalTo: button.rightAnchor, constant: 12).isActive = true 25 // 中央にする 26 //button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true 27 //丸みに対して 28 button.layer.cornerRadius = 10 29 button.layer.masksToBounds = true 30 31 cell.contentView.heightAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true 32 33 //ラベルについて 34 var label = UILabel() 35 label.backgroundColor = UIColor.darkGray 36 label.text = "120×10" 37 // itemに格納した値を設定 38 if indexPath.row < item.count { 39 label.text = item[indexPath.row] 40 } 41 42 //丸みに対して 43 label.layer.cornerRadius = 5 44 label.layer.masksToBounds = true 45 cell.contentView.addSubview(label) 46 47 label.translatesAutoresizingMaskIntoConstraints = false 48 cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true 49 cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true 50 51 //アラート 52 func addalert()-> String{ 53 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 54 55 // OKボタンの設定 56 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 57 (action:UIAlertAction!) -> Void in 58 59 // OKを押した時入力されていたテキストを表示 60 if let textFields = alert.textFields { 61 62 // アラートに含まれるすべてのテキストフィールドを調べる 63 for textField in textFields { 64 65 self.item.insert(textField.text!, at: 0) 66 self.label.text = textField.text! 67 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 68 print(textField.text!) 69 } 70 } 71 }) 72 alert.addAction(okAction) 73 74 // キャンセルボタンの設定 75 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 76 alert.addAction(cancelAction) 77 78 // テキストフィールドを追加 79 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 80 textField.placeholder = "テキスト" 81 }) 82 83 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 84 85 // アラートを画面に表示 86 self.present(alert, animated: true, completion: nil) 87 } 88 return cell 89 } 90 91@IBAction func addlabel(_ sender: Any) { 92 93 alert() 94 95 96 } 97 98 99 override func viewDidLoad() { 100 super.viewDidLoad() 101 102 } 103 104 105 106 override func didReceiveMemoryWarning() { 107 super.didReceiveMemoryWarning() 108 } 109 110 111}
やりたいこと
cellの高さの間隔を一定にしたい
困っていること
rowheightなどで高さを固定してもどうしても高さの違うcellが入ってしまう。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/04 08:15
退会済みユーザー
2018/05/04 08:15