4
4

More than 3 years have passed since last update.

UITableViewにUIButtonを追加する

Posted at

UITableViewの追加方法と、Cellにボタンを追加する方法を記載します。

UITableViewの作成

UITableView(frame: CGRect, style: UITableView.Style)でUITableViewを作成します。
CGRect(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat)でテーブルのサイズを指定し、
self.view.addSubview()でテーブルを画面に追加します、

var tableView: UITableView = UITableView()

override func viewDidLoad() {
     super.viewDidLoad()

     // 画面の横の長さ
     let width = self.view.frame.size.width
     // 画面の縦の長さ
     let height = self.view.frame.size.height

     tableView = UITableView(frame: self.view.frame, style: UITableView.Style.grouped)
     tableView.dataSource = self
     tableView.delegate = self
     tableView.sizeToFit()
     // tableViewのサイズ指定
     tableView.frame = CGRect(x:0, y:50, width:Int(width), height: Int(height))
     // 追加
     self.view.addSubview(tableView)
}

UITableViewを作成するばあ、row数とsection数の指定も必要になります。

// section数
public func numberOfSections(in tableView: UITableView) -> Int {
     return 1
}

// row数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 9
}

Cellの中身を追加

UITableViewのCellを内容を追加します。
Cellに、UItextField、UIButton等の追加ができます。
追加時にタグにindexPathを入れることで、選択時にどこを選択しているのか判断できます。

// Cellの高さ
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     return 50
}

// Cellの内容   
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

     if cell == nil {
          cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")
     } else {
          self.view.willRemoveSubview(cell!)
          cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")
     }

     let width = self.view.frame.size.width

     // UITextField     
     let textfi = UITextField()
     textfi.delegate = self
     textfi.tag = indexPath.row
     textfi.frame = CGRect(x:50, y:0, width:width - 50, height: 50)
     cell?.addSubview(textfi)

     // UIButton
     let addbutton = UIButton()
     addbutton.tag = indexPath.row
     // 押下時動作
     addbutton.addTarget(self, action: #selector(buttonEvemt), for: UIControl.Event.touchUpInside)
     addbutton.frame = CGRect(x:0, y:0, width:50, height: 50)
     cell?.addSubview(addbutton)
     return cell!
}

Cellの選択時の動作

Cellを選択してときの動作を追加できます。
UIButton、UITextField等は画面に追加したときと同じ用に記載すればいいですが、
どのCellを選択しているかわからないため、tagに入れたindexPath.rowでどれを選択しているか
判断できるようにする必要があります。

// Cell選択時動作
func tableView(_ table: UITableView, didSelectRowAt indexPath: IndexPath){

}

// UIButtonを選択した時
@objc func buttonEvemt(_ sender: UIButton) {
     print("buttonTag", sender.tag)
}

// UITextFieldを選択した時
func textFieldDidBeginEditing(_ textField: UITextField) {
     print("start",textField)
     print("tag",textField.tag)
}

// UITextFieldを離した時
func textFieldDidEndEditing(_ textField: UITextField) {
     print("end",textField)
     print("tag",textField.tag)
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4