UITableViewCell with UITextField


Gerriet M. Denkmann
 

iOS 12, Xcode 10

I have a subclass of UITableViewController.

The UITableView should use UITableViewCells with UITextFields (for data entry).

So I created MyTableViewCellWithTextField which has:
@property IBOutlet UITextField *textField;
Changed the class of the Prototype Cell to MyTableViewCellWithTextField
added a UITextField to the ContentView and tried to connect the outlet to this textField.

But no luck.

So I have to do:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId forIndexPath: indexPath];

UITextField *badHack = cell.contentView.subviews.firstObject;
badHack.keyboardType = …
badHack. placeholder = …
badHack. text = …
}

But I have a nagging feeling that this is *not* quite the right way.

What should I do instead ?

Gerriet.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.