我想創建一個窗體樣式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!
}
}
你使用故事板? –
我沒有這個,但我可以做。我接受所有建議。 – Catherine
我認爲本教程將幫助您:https://www.andrewcbancroft.com/2015/02/12/custom-uitableviewcell-text-input-swift/ – 3stud1ant3