2017-07-30 11 views
1

セルの左側にラベルが、右側にTextFieldというフォームスタイルのTableViewを作成したいと思います。カスタムTableViewCellにUITextFieldを追加する

しかし、TextFieldの内部カスタムTableViewCellsに関する情報を見つけるのが困難です。それも可能ですか?おそらくxibファイルだと思っていましたが、そこに回答を見つけるのはあまり幸運ではありません。

ガイダンスに感謝します。情報を見つけて、レイアウトに

を作成するためのstroyboardを使用する

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! 

    } 


} 
+0

ストーリーボードを使用していますか? –

+0

私はこれを持っていないが、私はすることができます。私はすべての提案に開放されています。 – Catherine

+0

私はこのチュートリアルがあなたを助けてくれると思います:https://www.andrewcbancroft.com/2015/02/12/custom-uitableviewcell-text-input-swift/ – 3stud1ant3

答えて

-3

使用プロトコルは、それが役立つことを願っています!

0

これは完全に可能です。この手順、次の試してみてください。

  1. は、あなたのTableViewControllerはあなたのセルをデキューするときのために、テキストフィールドのデリゲートを設定UITextFieldDelegate
  2. に準拠して作成します
  3. (あなたがNIBやストーリーボードを使用することができます)あなたのUITableViewCellにあなたのUILabelで、UITextFieldを追加します。 didSelectRowであなたのTableViewController
  4. あなたのテキストフィールドがfirstResponderになるようにします(テキストフィールドに正確にタップする必要はありません)

はヘクタールないでくださいここでコンピュータをテストしてみましょう。しかし、これが役に立ったら教えてください

関連する問題