ELCTextFieldCell – A Useful TableViewCell for Forms

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
January 4th, 2011 Posted by: (ELC) - posted under:Featured » Snippets

Hello iCoders, today I am going to be open sourcing a UITableViewCell we have created over at ELC that we have found to be very useful. When developing apps we have found that many times a form of some type is required. This is common in Registration forms, contact forms, feedback forms, etc. The problem was writing the same basic code over and over to have an elegant fast form experience for the user. To quicken the development time of these elements we created the ELCTextFieldCell class which facilitates the creation and flow of a larger form. In this post I will be walking you through the usage of the class.

GIT Hub

You can find a demo project with this code hosted on GitHub. Follow us if you find it useful, we try to open source as much as we can.

Screencast

Here is a fast video demo of a simple form that can be made using these cells.

ELCTextFieldCell Demo from Collin Ruffenach on Vimeo.

How to use

This class is best used by defining to arrays in the header of the table view controller that is going to use it. I like to call mine labels and placeholders. In these arrays store the values that are going to be the left labels for your forms cells and the placeholders for the cells. I create these in the viewDidLoad method most commonly. The reason I like to do this is because it allows for less code to be written through many other method in the table view controller. From here we need to define 6 more methods, most of which are pre defined by Apple in your table view controller class.

 
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [labels count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"Cell";
 
    ELCTextfieldCell *cell = (ELCTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[ELCTextfieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
 
	[self configureCell:cell atIndexPath:indexPath];
 
    return cell;
}

Here I like to have a configureCell:AtIndexPath method to keep my delegate UITableViewCellDelegate methods a bit cleaner. In this case my 4th cell (row 3) is for a phone number, you can obviously change this depending on your needs.

- (void)configureCell:(ELCTextfieldCell *)cell atIndexPath:(NSIndexPath *)indexPath {
 
	cell.leftLabel.text = [self.labels objectAtIndex:indexPath.row];
	cell.rightTextField.placeholder = [self.placeholders objectAtIndex:indexPath.row];
	cell.indexPath = indexPath;
	cell.delegate = self;
 
	if(indexPath.row == 3) {
 
		[cell.rightTextField setKeyboardType:UIKeyboardTypeNumberPad];
	}
}

All that is left to do is implement the ELCTextFieldDelegate methods. The first we will implement is the method that will be called whenever text is changed within the text field of the cell. In this case I just print out all the data going through this method but in most cases you will be applying these changes to your model.

- (void)updateTextLabelAtIndexPath:(NSIndexPath*)indexPath string:(NSString*)string {
 
	NSLog(@"See input: %@ from section: %d row: %d, should update models appropriately", string, indexPath.section, indexPath.row);
}

The final method that you implement will be the method called when return is hit by the user when typing in the rightTextField of the cell. Here you will first check if it is anything but the bottom most cell. If it is, you will increment the IndexPath row and then request that the rightTextField of this next cell becomes the new first responder, which will make the field active. Then you tell the tableview to make sure that this new index path cell is visible. With long forms the tableview will scroll with the users navigation through each field which is very nice. If it is indeed returning from the last cell all we do is tell the textFieldCell to resign first responder so the keyboard disappears.

-(void)textFieldDidReturnWithIndexPath:(NSIndexPath*)indexPath {
 
	if(indexPath.row < [labels count]-1) {
		NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
		[[(ELCTextfieldCell*)[self.tableView cellForRowAtIndexPath:path] rightTextField] becomeFirstResponder];
		[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
	}
 
	else {
 
		[[(ELCTextfieldCell*)[self.tableView cellForRowAtIndexPath:indexPath] rightTextField] resignFirstResponder];
	}
}

That’s all and you’re done! This should be a super fast way to make forms in your apps. Let me know any questions and happy iCoding.

Follow me on twitter @cruffenach

  • http://icode.dreamvision-soft.com/blog/?p=95 ELCTextFieldCell – A Useful TableViewCell for Forms | iCode

    [...] Original post on iCodeBlog [...]

  • http://scoritz.com/iphone-ipad-ios-developement/elctextfieldcell-%e2%80%93-a-useful-tableviewcell-for-forms/ ELCTextFieldCell – A Useful TableViewCell for Forms | SCORITZ

    [...] the rest here: ELCTextFieldCell – A Useful TableViewCell for Forms Bookmark on Delicious Digg this post Recommend on Facebook share via Reddit Share with Stumblers [...]

  • rahulvyas

    @cruffenach
    Hey Nice tutorial and excellent work. By the way how about adding a done button on numberPad Keyboard. Sometimes it’s very difficult to add done button on keyboard. It would be great if add a done button within current sample.

    Thanks
    Rahul Vyas

  • http://fruitandrobots.wordpress.com/ Jason Nezumi

    Wow, this is such a well written solution! I’ve been a big fan of generically created expansion components, because really, everybody tends to come across some situations that they’re asked to code a lot. I think I have a new tool in my kit now for whenever I have to make a form–no more laying out fields on a scrollview with a plain grey background!

    One question, though I think I can guess the answer–has it been tested with grouped table views? I imagine that using these cells in a grouped view could make some very attractive settings and profile pages.

  • http://sunflowerapps.com SFA

    Hi, thanks for sharing the code, I tested this with Group Table View and it works great.

  • Kunal

    nice tutorial. Would have been great if stored data in some db, and included more properties like UIPikcerView etc.

  • Klarity

    I got this all set up, but I’m havin troubles with getting the data out of the TextFields, could you give me/us a sample of doing so?

    thanks =)

  • Brent

    This has a bug in that sometimes rows (primarily the first and last rows) can switch their rightTextField text values when both get scrolled off screen. More rows need to be added and the landscape orientation needs to be enabled in the demo to observe this.

  • Shariffs

    Hi

    I saw you code example regarding ELCTextFieldCell, I must say it was very helpfull. I am just wondering if you can help me with the issue I am facing now a days.

    I have create a community information App. for iphone. I am using MYSQL as a database and I am able to save and retrieve the data in database related to the members, Except the member’s image. The image is in iphone photo folder, I am able to display the photo on view but have no idea how to save this image in database along with other information.

    Please provide the sample code.

    Thanking you in anticipation.

    Sana

  • Sajithpp

    I have same issue, I am using MYSQL database and trying to save image in the database(in BLOB field) for my iphone app. Any help will be appreciated.

    Sajith

  • Yu

    White color is pretty appreciably synonymous with pearls but they do arrive in other eye-catching colours just like black, pink, lavender, golden and gray etc. There are 4 types of pearls plus they are freshwater, Akoya seawater, http://www.koopthomassabo.com/thomas-sabo-bedels.html Tahitian and South seawater ones. Freshwater and akoya market extra than other styles usually for their reduce expenses and color brilliance.

  • Viraj

    You save my day. Great tutorial.

  • http://www.facebook.com/people/Shruti-Seo/100002398587779 Shruti Seo

    iPhone
    Application Development India is iphone mobile software development company
    provides iPhone application development,Hire iphone developer, iphone 4/3GS and
    iPad Developer. iPhone Application Development by Connect Infosoft`s Developer
    Hire iPhone application developer for your iPhone application development
    requirements. iPhone Game Development,Mobile Application Developer. Hire iPhone
    Mobile Application Developer Programmer, iPhone 4 Game Development India
    Development.
     

  • http://www.facebook.com/profile.php?id=1607195829 Jack Haskell

    I am trying to figure out a way to discover which cell is active in

    -(void)textFieldDidReturnWithIndexPath:(NSIndexPath*)indexPath
    What I want to do is push another view controller with a search table used to fill in the cell. Is this possible?

    Very nice control by the way. Makes it very easy to populate a form and update a Db or DataModel.

  • http://www.facebook.com/profile.php?id=1607195829 Jack Haskell

    I am trying to figure out a way to discover which cell is active in

    -(void)textFieldDidReturnWithIndexPath:(NSIndexPath*)indexPath
    What I want to do is push another view controller with a search table used to fill in the cell. Is this possible?

    Very nice control by the way. Makes it very easy to populate a form and update a Db or DataModel.

  • Raviatri

    hi…to my bros from the world….
    i have a query is this possible to add a Search Bar in a UIToolBar?
    i did a lot of search bt cant find the answer .when i adding th search bar in toolbar in interface builder it gives an error in interface builder i.e. Error: Search Bar, id 33. Illegal Configuration; UISearchBars are not supported in UIBarButtonItems in non-iPad documents.

    please help me to find the answer.
    thanks

blog comments powered by Disqus
canakkale canakkale canakkale balik tutma search canakkale vergi mevzuati