質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

1490閲覧

コードのみでtableviewのセル内にtextfieldを配置し、入力できるようにしたい。

moaikuntvs

総合スコア23

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2019/11/21 09:36

swift4のtableviewを表示して、セルごとに入力画面を用意したいのですが、コードのみの実装したパターンが検索しても出てきません。
どうかご教授ください。

swift

1 extension addview: UITableViewDataSource { 2 3 // セクションごとにデータ要素数 4 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 5 return data.count 6 } 7 8 /*// セクション数 9 func numberOfSections(in tableView: UITableView) -> Int { 10 return sectionName.count 11 } 12 13 // セクションヘッダ 14 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 15 return sectionName[section] 16 } 17 */ 18 19 // セルの高さ 20 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 21 return 40 22 } 23 24 // セル生成 25 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 26 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 27 28 cell.textLabel?.text = data[indexPath.row] 29 30 date.frame = CGRect(x: cell.frame.midX, y: cell.frame.minY, width: view.frame.width - 220, height: 30) 31 //改行ボタンを完了ボタンに変更 32 date.returnKeyType = .done 33 //文字が何も入力されていない時に表示される文字(薄っすら見える文字) 34 //textfield.placeholder = "FPS" 35 //viewにtextfieldをsubviewとして追加 36 view.addSubview(date) 37 38 39 //cell.accessoryType = .disclosureIndicator 40 //cell.accessoryView = UISwitch() // スィッチ 41 42 return cell 43 } 44 45 46 } 47

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

自分の場合はコードでカスタムセルを作ってます!
titleLabel などの部分をtextFieldに変えてみたらよいかと。
frameで位置を指定するよりオートレイアウトを使った方が自由度も高いです。

Swift

1final class MenuTableViewCell: UITableViewCell { 2 private let iconImageView: UIImageView = { 3 let imageView = UIImageView() 4 imageView.translatesAutoresizingMaskIntoConstraints = false 5 imageView.layer.cornerRadius = imageView.frame.width / 2 6 imageView.layer.masksToBounds = true 7 imageView.isUserInteractionEnabled = true 8 return imageView 9 }() 10 11 private let titleLabel: UILabel = { 12 let label = UILabel() 13 label.translatesAutoresizingMaskIntoConstraints = false 14 label.font = UIFont.systemFont(ofSize: 20, weight: .medium) 15 label.textAlignment = .center 16 label.textColor = .darkGray 17 label.numberOfLines = 0 18 return label 19 }() 20 21 22 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { 23 super.init(style: style, reuseIdentifier: reuseIdentifier) 24 addSubview(iconImageView) 25 addSubview(titleLabel) 26 addSubview(bottomLineView) 27 28 let constraints = [ 29 iconImageView.leftAnchor.constraint(equalTo: leftAnchor, constant: 8), 30 iconImageView.centerYAnchor.constraint(equalTo: centerYAnchor), 31 iconImageView.heightAnchor.constraint(equalToConstant: 30), 32 iconImageView.widthAnchor.constraint(equalTo: iconImageView.heightAnchor), 33 titleLabel.leftAnchor.constraint(equalTo: iconImageView.rightAnchor, constant: 16), 34 titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor) 35 ] 36 37 NSLayoutConstraint.activate(constraints) 38 } 39 40 required init?(coder: NSCoder) { 41 fatalError("init(coder:) has not been implemented") 42 } 43 44 func setTitle(_ text: String) { 45 titleLabel.text = text 46 } 47}

Swift

1extension addView: UITableViewDataSource { 2 // セル生成 3 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 4 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MenuTableViewCell 5 cell.setTitle(data[indexPath.row]) 6 return cell 7 } 8}

viewDidLoadなどでtableView.register(MenuTableViewCell.self, forCellReuseIdentifier: "Cell")などとしてカスタムセルを登録することも忘れないようにして下さい!

投稿2019/11/21 13:27

fathy

総合スコア254

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問