Adding a text field to a UITableview Cell

前端 未结 3 410
孤街浪徒
孤街浪徒 2020-12-22 08:43

Am new to Ios programming with swift 2.2

What am trying to achieve is inserting two text fields in a uitableviewcell or uitableviewcells

for example the tex

3条回答
  •  自闭症患者
    2020-12-22 09:46

    1. Create a subclass of UITableViewCell with two text field outlets:

      class TextFieldsCell: UITableViewCell {
          @IBOutlet var textfield1: UITextField!
          @IBOutlet var textfield2: UITextField!
      }
      
    2. In IB, select your table view and add a prototype cell to your table view by setting "Prototype Cells" to "1", in attributes inspector:

    1. In IB, layout the text fields inside your cell using autolayout. (skipping details of autolayout implementation)

    1. In IB, select your table view cell, and change it's "Identifier" to "TextFieldsCell", in attributes inspector:

    1. In IB, select your table view cell, and change it's "Class" to "TextFieldsCell", in identity inspector:

    1. In IB, connect TextFieldsCell's IBOutlets with the corresponding text fields:

    1. In your view controller which contains the table view, implement the following:

      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 2;
      }
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          let cell: TextFieldsCell = tableView.dequeueReusableCellWithIdentifier("TextFieldsCell") as! TextFieldsCell
          return cell
      }
      
    2. If you set everything up properly, you should see this on the next run:

提交回复
热议问题