9

Is there a standard way to set up a table to allow editing-in-place, kind of like this:

enter image description here

I only need editable text at the moment, but I might need UISwitches or UISliders in the future.

Isuru
  • 30,617
  • 60
  • 187
  • 303
cannyboy
  • 24,180
  • 40
  • 146
  • 252

2 Answers2

18

Yep. Just add a UITextField, with its font and textColor set to appropriate values, as subviews of the table cell's contentView. You probably want to give the field a tag as well, so that you can easily grab a reference to it using the contentView's -viewWithTag: method.

With short forms you can get away with keeping an array of cells, one for each field, and handing them off to the table view without going through the -dequeueReusableCellWithIdentifier: mechanism, but if you've got a lot of stuff to enter then it gets more complicated. In that case, you probably want to assign a different reuse identifier to each type of cell—one for text-field cells, one for switch cells, one for slider cells, etc. Once you've dequeued or created the row's cell, you'd then grab its control from the content view (as above) and set its value from wherever you've stored that.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • 1
    Thanks! And how to end editing the cell if I click outside the TableView? – Rudolf J May 12 '14 at 12:11
  • Actually adding them as subviews of the `contentView` won't make it look like in the image. It'll go under the textlabel also. I set the textfield as the `accessoryView` and it worked. However you have to hardcode the width of it as a magic number and it's an issue if you're supporting both screen orientations. – Isuru Aug 09 '14 at 18:27
0

Edit in-place using a "Content: Static Cells" and make it look like a default styled UITableviewCell.

  1. In UITableView create two cells: one Custom and one Subtitle Styled.
  2. Copy the "title" label corresponding to the Subtitle styled UITableViewCell to the Custom UITableViewCell.
  3. In Custom UITableViewCell, drop a UITextField. Copy in this the style parameters from the Detail UILabel from Subtitle Styled UITableViewCell in this UITextField.
  4. Do the necessary arrangements.

The looks pretty equal, aren't they?

Now, do the IBoutlet stuff.

CesareoAguirre
  • 1,557
  • 13
  • 11