Ich möchte ein Form-Format TableView mit einem Label auf der linken Seite der Zelle und TextField auf der rechten Seite erstellen.Hinzufügen von UITextField zu benutzerdefinierter TableViewCell
Allerdings habe ich eine schwierige Zeit, um Informationen zu TextFields internen benutzerdefinierten TableViewCells zu finden. Ist es überhaupt möglich? Ich dachte mir vielleicht eine Xib-Datei, aber auch nicht viel Glück beim Finden von Antworten.
Sehr zu schätzen einige Leitlinien.
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!
}
}
Verwenden Sie Storyboards? –
Ich habe damit nicht, aber ich kann. Ich bin offen für alle Vorschläge. – Catherine
Ich denke, dieses Tutorial wird Ihnen helfen: https://www.andrewcbancroft.com/2015/02/12/custom-uitablecell-text-input-swift/ – 3stud1ant3