Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2008, 01:25 PM   #1 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 15
Default UITextField as a subview in a table cell

To clean up my user interface, I want to use a table view to group a set of UITextField instances. I know I'm fudging the functionality of table view (I don't have a list of data I'm managing, just a set of user-entered data), but table view provides me with the layout & group I'm looking for (and simply text fields don't).

Essentially, I'm adding the text field as a sub view on a table cell. Then when the user tabs into the text field, they'll be able to enter data from the keyboard.

Since the text field is a sub view in a table cell, do I have to add any special selection functionality on the cell, or will a tap into the text field automatically engage the keyboard?

-M
melii1983 is offline   Reply With Quote
Old 08-02-2008, 01:42 PM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default

You're not fudging functionality, Apple does this in many of the delivered applications, and there is a sample project called Editable Detail View or something like that that shows how to do it.

Tapping the field will bring up the keyboard, but the keyboard won't dismiss unless you do something. In this situation, I usually use a keyboard with a return key, and have it take them to the next method. I do this by binding the Did End on Exit event of the text field to an action method. Figuring out how to go to the next row is a little bit gnarley, this is one way to do it:

Code:
-(IBAction)textFieldDone:(id)sender
{
	UITableViewCell *cell = (UITableViewCell *)[[(UIView *)sender superview] superview];
	UITableView *table = (UITableView *)[cell superview];
	NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
	NSUInteger row = [textFieldIndexPath row];
	row++;
	if (row >= kNumberOfEditableRows)
		row = 0;
	NSUInteger newIndex[] = {0, row};
	NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
	UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:newPath];
	UITextField *nextField = nil;
	for (UIView *oneView in nextCell.contentView.subviews)
	{
		if ([oneView isMemberOfClass:[UITextField class]])
			nextField = (UITextField *)oneView;
	}
	[nextField becomeFirstResponder];
	
}
One thing to keep in mind is that if a row scrolls off the screen, the cell will go into the re-use pool, so if there's a chance our table will scroll, make sure you register as the text field's delegate and keep track of the changes so that if the field rolls off and gets re-used, you haven't lost the value it held.
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 08-03-2008, 10:02 PM   #3 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 15
Default

>> Tapping the field will bring up the keyboard, but the keyboard won't dismiss
>> unless you do something. In this situation, I usually use a keyboard with
>> a return key, and have it take them to the next method. I do this by binding
>> the Did End on Exit event of the text field to an action method.

This is going to be a very stupid question, but how do I do this binding programmatically? Because UITextField is a subview of the cell, I haven't figured out a way to get this set up in IB.

-M
melii1983 is offline   Reply With Quote
Old 08-03-2008, 10:46 PM   #4 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 19
Default

Quote:
Originally Posted by melii1983 View Post
>> Tapping the field will bring up the keyboard, but the keyboard won't dismiss
>> unless you do something. In this situation, I usually use a keyboard with
>> a return key, and have it take them to the next method. I do this by binding
>> the Did End on Exit event of the text field to an action method.

This is going to be a very stupid question, but how do I do this binding programmatically? Because UITextField is a subview of the cell, I haven't figured out a way to get this set up in IB.

-M
You can drag the UITextField into the NIB file and create it that way!
Garrett is offline   Reply With Quote
Old 08-04-2008, 03:29 PM   #5 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 15
Default

Well, I've figured out something that works...

I've created a subclass of UICell that has a UITextField as a property.

In the "cellForRowAtIndexPath" method in the view controller, I then set the delegate for the text field to the view controller.

In then have the view controller adopt the UITextFieldDelegate protocol and implement the method "(BOOL)textFieldShouldReturn" in the view controller. Within this method I update the model and resign firstResponder

When I enter data into the text field and hit "return", the model is updated and the keyboard goes away. <yeah!>

-M
melii1983 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 157,864
Threads: 88,916
Posts: 379,299
Top Poster: BrianSlick (7,072)
Welcome to our newest member, MediaSolutions
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0