0

I need to programatically add a text field input into a table view cell. How could I do this in cellForRowAtIndexPath?

laxj11
  • 117
  • 1
  • 8

3 Answers3

2

I've found this tutorial really helpful for creating xib (nib) based table cells. Then you can add whatever you like in each cell row.

http://clingingtoideas.blogspot.com/2011/03/uitableview-how-to-part-5-more-about.html

Kolya Miller
  • 185
  • 4
  • 12
1

You can set it as one of the views within the row - for example, as the accessory view:

UITextField* input = [[UITextField alloc] initWithFrame:CGRectMake(5,5,80,20)];
[input setDelegate:self];
[cell setAccessoryView:input];
[input release];

Then you SHOULD be able to access the value with [(UITextField*)[[tableView cellForRowAtIndexPath:indexPath] accessoryView] text]; Make sure you set up that delegate info on whichever object you make the delegate.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • 1
    You could also add it as a new subview within the cell, but if you're doing that it's best to create your own subclass of UITableViewCell; that way you can use Interface Builder to set it up as well. – Tim May 18 '11 at 21:53
  • When I set it as the delegate, It says that self isnt an appropriate delegate. Is there somehting I need to add? – laxj11 May 19 '11 at 00:00
  • @laxj11 When setting up the delegate, in the main class's .h file you should make it look like so: `@interface YourClassNameHere : UITableViewController {` – Tim May 21 '11 at 19:31
1

This has been answered before (1, 2), but the best place to start in my opinion is the Table View Programming Guide, where you can find three different methods regarding cell customization.

Community
  • 1
  • 1
phi
  • 10,634
  • 6
  • 53
  • 88