将UITextField添加到自定义TableViewCell

时间:2017-07-30 04:15:51

标签: ios uitableview

我想创建一个表单样式TableView,单元格左侧是标签,右侧是TextField。

但是,我很难在自定义TableViewCells内的TextField上查找信息。它甚至可能吗?我想也许是一个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!

    }


}

2 个答案:

答案 0 :(得分:0)

这是完全可能的。请尝试以下步骤:

  1. 将您的UILabel和UITextField添加到您的UITableViewCell(您可以使用NIB或Storyboard)
  2. 使您的TableViewController符合UITextFieldDelegate
  3. 当您将单元格出列时,将文本字段的委托设置为TableViewController
  4. 在didSelectRow中,使textField成为firstResponder(因此您无需在textField上精确点击)
  5. 这里没有电脑可以测试,但如果有帮助请告诉我

答案 1 :(得分:-3)

使用协议查找信息并使用stroyboard创建布局

希望它会有所帮助!

相关问题