将UITextField添加到自定义TableViewCell
问题描述:
我想创建一个窗体样式TableView,单元格左边的标签和右边的TextField。将UITextField添加到自定义TableViewCell
但是,我很难找到关于TextField内部自定义TableViewCells的信息。它甚至有可能吗?我想也许是一个xib文件,但没有太多的运气找到答案。
非常感谢一些指导。
import UIKit
class AddProfileViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let sectionTitles: [String] = ["PROFILES", "IDENTIFICATION", "EMERGENCY CONTACTS"]
let sectionImages: [UIImage] = [#imageLiteral(resourceName: "arrow"), #imageLiteral(resourceName: "arrow"), #imageLiteral(resourceName: "arrow")]
let s1Data: [String] = ["Barn Name", "Show Name", "Color", "Gender", "Breed", "Birth Date", "Heigh (Hh)"]
let s2Data: [String] = ["Markings", "Tattoo", "Branding", "Microchip ID", "Passport", "Registration", "Registration #"]
let s3Data: [String] = ["Contact Name", "Contact Phone", "Vet Name", "Vet Phone", "Farrier Name", "Farrier Phone"]
var sectionData: [Int: [String]] = [:]
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationItem.rightBarButtonItem = self.editButtonItem
sectionData = [0:s1Data, 1:s2Data, 2:s3Data]
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 45
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = UIColor.white
let label = UILabel()
label.text = sectionTitles[section]
label.frame = CGRect(x: 15, y: 5, width: 200, height: 35)
label.font = UIFont(name: "Avenir", size: 15)
label.textColor = UIColor.darkGray
view.addSubview(label)
let image = UIImageView(image: sectionImages[section])
image.frame = CGRect(x: 300, y: 8, width: 25, height: 25)
image.contentMode = .scaleAspectFit
view.addSubview(image)
return view
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell");
}
cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]
cell!.textLabel?.font = UIFont(name: "Avenir", size: 13)
cell!.textLabel?.textColor = UIColor.lightGray
return cell!
}
}
答
使用协议来寻找信息,并使用stroyboard创建布局
希望这将有助于!
答
这是完全可能的。试试下面这个步骤:
- 您的UILabel,同时也是UITextField添加到您的UITableViewCell(你可以用笔尖或故事板)
- 让您TableViewController符合UITextFieldDelegate
- 当你出列的单元格,设置你的文本字段的委托您TableViewController
- 在didSelectRow让你的文本框变得firstResponder(所以你不必精确地轻触文本框)
别哈有一台电脑在这里测试,但让我知道这是否有帮助
你使用故事板? –
我没有这个,但我可以做。我接受所有建议。 – Catherine
我认为本教程将帮助您:https://www.andrewcbancroft.com/2015/02/12/custom-uitableviewcell-text-input-swift/ – 3stud1ant3