So, I am creating a login screen with UITableView. There are two textfields for email and password. When the user presses 'Login' button, I want to store the contents of those two textfields in two NSStrings. How do I do that? Here is the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITextField *tf = [[UITextField alloc] init];
UITableViewCell *cell = [self.tView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = #"Email";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
tf = jidField = [self makeTextField:#"" placeholder:playerTextField.placeholder];
[cell addSubview:jidField];
}
else {
playerTextField.placeholder = #"Password";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
//playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
playerTextField.textAlignment = NSTextAlignmentLeft;
// playerTextField.tag = 0;
//playerTextField.delegate = self;
playerTextField.clearButtonMode = YES; // no clear 'x' button to the right
[playerTextField setEnabled: YES];
[cell addSubview:playerTextField];
}
}
return cell;
}
In cellForRowAtIndexPath, set tag for userid and password field and retrieve the textfields with its tag identifier. Please find following implementation of loginAction method.
- (void)loginAction:(id)sender {
NSIndexPath *userIdRow = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *passwordFieldRow = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *userIdTableCell = (UITableViewCell *)[tableview cellForRowAtIndexPath:userIdRow];
UITableViewCell *passwordFieldTableCell = (UITableViewCell *)[tableview cellForRowAtIndexPath:userIdRow];
UITextField *userIDField = (UITextField *)[userIdTableCell viewWithTag:1001];
UITextField *passwordField = (UITextField *)[passwordFieldTableCell viewWithTag:1002];
// You can store now text to either your instance variables or properties in the following statements
NSLog(#"%#", userIDField.text];
NSLog(#"%#", passwordField.text];
}
In cellForRowAtIndexPath, you have to tag your's textfields
if ([indexPath row] == 0) {
playerTextField.tag = 1001;
} else {
playerTextField.tag = 1002;
}
Just like Tarek said, this is how you do it.
In your *.h file
#interface v1ViewController : UITableViewController
{
UITextField IBOutlet *playerEmailTxt;
UITextField IBOutlet *playerPassword;
}
#property (nonatomic, retain) UITextField IBOutlet *playerEmailTxt;
#property (nonatomic, retain) UITextField IBOutlet *playerPassword;
In your *.m file
...
#synthesize playerEmailTxt;
#synthesize playerPassword;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITableViewCell *cell = [self.tView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 1)
{
if (indexPath.row == 0)
{
playerEmailTxt = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
playerEmailTxt.adjustsFontSizeToFitWidth = YES;
playerEmailTxt.textColor = [UIColor blackColor];
playerEmailTxt.placeholder = #"Email";
playerEmailTxt.keyboardType = UIKeyboardTypeEmailAddress;
playerEmailTxt.returnKeyType = UIReturnKeyNext;
[cell addSubview:playerEmailTxt];
}
else if (indexPath.row == 1)
{
playerPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
playerPassword.adjustsFontSizeToFitWidth = YES;
playerPassword.textColor = [UIColor blackColor];
playerPassword.placeholder = #"Password";
playerPassword.keyboardType = UIKeyboardTypeDefault;
playerPassword.returnKeyType = UIReturnKeyDone;
playerPassword.secureTextEntry = YES;
[cell addSubview:playerPassword];
}
}
return cell;
}
Now add your login action
-(IBAction)LoginAction
{
NSMutableString *usrEmailStr = [NSMutableString stringWithFormat:#"%#", playerEmailTxt];
NSMutableString *usrPasswdStr = [NSMutableString stringWithFormat:#"%#", playerPassword];
//Do whatever you want with these strings
}
I'd also recommend you check out the free Sensible TableView framework. Seems like a perfect fit for what you're trying to do, since the framework will automatically fetch the data from your fields and store them in any data structure you wish.
Related
I have a UITableView that I used for username/password enter.
But I have problem to get values when I click on button.
This is how I make table:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = #"example#gmail.com";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
}
else {
playerTextField.placeholder = #"Required";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = NSTextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
playerTextField.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:playerTextField];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = #"Email";
}
else {
cell.textLabel.text = #"Password";
}
}
return cell;
}
And this is what I have tried in button tap:
...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
UITextField* aField = (UITextField *)[aCell viewWithTag:0];
NSLog(aField.text);
}
..
This prints me just: Email Password (labels not data that I entered).
How to get entered values here?
All views default their tag to 0. Because of this, [aCell viewWithTag: 0] is returning the first view with tag zero - in this case the cell's textLabel - which is a UILabel that also responds to .text. If you set the tags in your cellForRowAtIndexPath: to something other than zero it should start working for you.
In that case you can give the tag to textfield like playerTextField.tag = 115 and you can get view by tag like UITextField *aField = (UITextField *)[aCell viewWithTag:115]; and display NSLog(#"text:%#",aField.text);
When I am entering text in textfield 1 that text is also displayed in textfield 9.
here is some code what i did to create textfield in tableview cell.
i added textfield in tableview cell .like below in cellforRowatintexpath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCell";
//UILabel* nameLabel = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text = [labels objectAtIndex:indexPath.row];
nameLabel.font = [UIFont boldSystemFontOfSize:12];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.tag =1;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
[nameLabel release];
//adding textfield to tableview cell.
tv = [[UITextField alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+2, 10.0, cell.contentView.bounds.size.width, 30)];
tv.tag = indexPath.row + 2;
//tv.text =[labels objectAtIndex:indexPath.row];
tv.font = [UIFont boldSystemFontOfSize:12];
//this method will take text from textfield of tableview cell using their individual tag
[tv addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:tv];
[tv setDelegate:self];
[tv release];
return cell;}
and here is the method getTextFieldValue method
- (void) getTextFieldValue:(UITextField *)textField{
if (textField.tag == 2) {
//NSString *value1 = [NSString stringWithFormat:#"%#",textField.text];
//NSLog(#"value1 is %#",value1);.
NSLog(#"2--->%#",textField.text);
}else if(textField.tag == 3){
NSLog(#"3--->%#",textField.text);
}else if(textField.tag == 4){
NSLog(#"4--->%#",textField.text);
}
else if(textField.tag == 5){
NSLog(#"5--->%#",textField.text);
}
else if(textField.tag == 6){
NSLog(#"6--->%#",textField.text);
}
else if(textField.tag == 7){
NSLog(#"7--->%#",textField.text);
}
else if(textField.tag == 8){
NSLog(#"8--->%#",textField.text);
}
else if(textField.tag == 9){
NSLog(#"9--->%#",textField.text);
}
else if(textField.tag == 10){
NSLog(#"10--->%#",textField.text);
}
else if(textField.tag == 11){
NSLog(#"11--->%#",textField.text);
}
else if(textField.tag == 12){
NSLog(#"12--->%#",textField.text);
} }
First of all, please rewrite your getTextFieldValue method like this:
- (void)getTextFieldValue:(UITextField *)textField {
NSLog(#"%d--->%#", textField.tag, textField.text);
}
You keep adding UITextFields to the cell, even when it's reused. Try putting that code inside the if statement.
You should set the text of the text field to nil every time though. Else it will still be there on dequeued cells (when scrolling).
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCell";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
//TODO: Set up nameLabel here
[cell.contentView addSubview: nameLabel];
[nameLabel release];
UITextField *tv = [[UITextField alloc] init];
tv.font = [UIFont boldSystemFontOfSize:12];
[tv setDelegate:self];
[tv addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:tv];
[tv release];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [labels objectAtIndex:indexPath.row];
[nameLabel sizeToFit];
return cell;
}
That is the retain problem is there.
You can take different cellIdentifier for each row. Only change one statement you get your solution.
Solve this problem with above method. i was facing same problem & i solve with this method.
I hope this will help you.
NSString *CellIdentifier = [NSString stringWithFormat:#"cellidentifier-%d",indexPath.row];
Maybe you have set some IBOutlet to the textField? Did you?
I am trying to implement UITableview based application. In my tableView their is 10 Section and each section having one row.
I want implement each section have Different type of ContentView(1-8 same ContentView 9th section Different ContentView). I did this code For that.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
UITextField *textField;
UITextView *textView;
NSUInteger section=[indexPath section];
if(section == 9){
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
[textView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor
]];
[textView setTag:([indexPath section]+100)];
[cell.contentView addSubview:textView];
}else{
textView=(UITextView*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}else {
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
[textField setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textField setTag:([indexPath section]+100)];
[cell.contentView addSubview:textField];
}else{
textField=(UITextField*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}
return nil;
}
My problem are:
1. After type some thing in the UITextField/UITextView i am scrolling in the UITableView. that time all data in the UITableViewCell(UITextField/UITextView) was lose, except last cell data.
2. If i create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Instead of
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Data will repeating . How can i over come this problem?
This line:
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Should never appear in your data source cellForRowAtIndexPath method.
Apart from that, your code is OK, except that you are not setting the text field value anywhere. You need a model (such as an array of strings for the 10 textfield values). This model should be updated when the textfields are edited, and in your method above you copy the value back out of the model and into the textfield's text property:
textfield.text = [self.modelArray objectAtIndex:indexPath.section];
The table pools and reuses cells in an unpredictable fashion, so that subview of a cell that just scrolled off the bottom might reappear next at the top, or might be disposed of.
This is why you saw it partially work. The cell's subviews work okay until their cell gets reused or unloaded, then things move to the wrong place or data disappears.
The solution is that your table's datasource needs to hold onto it's own data. This is usually an array representing your model. Your case is a little unusual because you are using the text controls in your table as inputs, rather than display, which is more typical.
I suggest doing it like this:
// in #interface
#property (nonatomic, retain) NSMutableArray *sections;
// in #implementation
#synthesize sections=_sections;
// at some point before the view appears
self.sections = [NSMutableArray array];
for (int i=0; i<10; i++) {
UIControl *textControl;
if (i<9) {
textControl=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
} else {
textControl=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
}
[textControl setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textControl setTag:i+100];
[sections addObject:textControl];
[textControl release];
}
Now your cellForRowAtIndexPath is a little simpler:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
NSUInteger section=[indexPath section];
if(section == 9) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil) {
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
}
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
}
}
// who knows what subview this cell has? it might not have one, or it might have the wrong one
// just clean it up to be certain
for (UIView *view in cell.subviews) {
[view removeFromSuperView];
}
// get the textControl we set up for _this_ section/cell
UIControl *textControl = [self.sections objectAtIndex:section];
// now we have a fresh cell and the right textControl. drop it in
[cell addSubview:textControl];
return cell;
}
hey the reason is you are doing this things when cell is nil ? but you are not writing any code when cell is not nil.
look at this example , in this example i am adding image view in tableview cell , hence you can add textviews or any other views like this
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgView;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)];
[imgView setImage:[UIImage imageNamed:#"img.png"]];
imgView.tag = 55;
[cell.contentView addSubview:imgView];
[imgView release];
}
else
{
imgView = (id)[cell.contentView viewWithTag:55];
}
so as showin here imgView = (id)[cell.contentView viewWithTag:55]; you have to give tag to you and write code showing above in else..
Let you try to make the cell labels and textviews by using following code. It works for me.
if (tagvalue ==3) {
static NSString *CellIdentifier = #"Cell3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
lbl7 = [[UILabel alloc] init];
[lbl7 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl7];
lbl7.backgroundColor = [UIColor clearColor];
lbl7.frame = CGRectMake(120, 5, 0, 40);
lbl7.tag = 70;
[lbl7 release];
lbl8 = [[UILabel alloc] init];
[lbl8 setFont:[UIFont boldSystemFontOfSize:18]];
[cell.contentView addSubview:lbl8];
lbl8.backgroundColor = [UIColor clearColor];
lbl8.textColor = [UIColor grayColor];
lbl8.frame = CGRectMake(120, 50, 0, 40);
lbl8.tag = 80;
[lbl8 release];
lbl7 = (UILabel*)[cell.contentView viewWithTag:70];
lbl8 = (UILabel*)[cell.contentView viewWithTag:80];
lbl7.text = [[rowsarray objectAtIndex:row]objectForKey:#"name"];
lbl8.text = [[rowsarray objectAtIndex:row]objectForKey:#"flavour"];
[lbl7 sizeToFit];
[lbl8 sizeToFit];
return cell;
}
if (tagvalue ==4) {
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
lbl9 = [[UILabel alloc] init];
[lbl9 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl9];
lbl9.backgroundColor = [UIColor clearColor];
lbl9.frame = CGRectMake(120, 5, 0, 40);
lbl9.tag = 90;
[lbl9 release];
lbl10 = [[UILabel alloc] init];
[lbl10 setFont:[UIFont boldSystemFontOfSize:18]];
[cell.contentView addSubview:lbl10];
lbl10.backgroundColor = [UIColor clearColor];
lbl10.textColor = [UIColor grayColor];
lbl10.frame = CGRectMake(120, 50, 0, 40);
lbl10.tag = 100;
[lbl10 release];
lbl9 = (UILabel*)[cell.contentView viewWithTag:90];
lbl10 = (UILabel*)[cell.contentView viewWithTag:100];
lbl9.text = [[rowsarray objectAtIndex:row]objectForKey:#"name"];
lbl10.text = [[rowsarray objectAtIndex:row]objectForKey:#"flavour"];
[lbl9 sizeToFit];
[lbl10 sizeToFit];
return cell;
}
I had same issue. Its not the problem with table class. The issue is at the place where you are calling this tableviewcontroller. First make the object of this call in .h and then allocate in .m, thats it..
When I was declaring in viewdidload like tbl *t = [self.storyboard...];, I was also facing the same problem. But when I put tbl *t; in .h problem solved.
I added uitextfield programatically to uitableview cell, the problem is when I type on the textfield and click done or move from the textfield the keyboard doesn't disappear.
Any suggestion to solve that?
#interface test_20110605ViewController : UIViewController <UITextFieldDelegate> {
UITextField *PtienttextField ;
}
#property ( nonatomic , retain ) UITextField *PtienttextField ;
#end
at m file
#implementation test_20110605ViewController
#synthesize PtienttextField ;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0 && indexPath.section == 1 ) {
//cell.textLabel.text = "Patient Name";
UILabel *PatientLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 25)];
PatientLabel.backgroundColor = [UIColor clearColor];
PatientLabel.tag = 1;
PatientLabel.text = #"Patient Name" ;
[cell.contentView addSubview:PatientLabel];
PtienttextField = [[UITextField alloc]initWithFrame:CGRectMake(140, 10, 400, 30)];
PtienttextField.clearsOnBeginEditing = YES;
PtienttextField.textAlignment = UITextAlignmentRight;
PtienttextField.returnKeyType = UIReturnKeyDone;
PtienttextField.delegate = self;
//PtienttextField.tag = 2 ;
[PtienttextField setBorderStyle:UITextBorderStyleBezel];
[cell.contentView addSubview:PtienttextField];
}
}
// Configure the cell.
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
#end
I think you're missing the following in textFieldShouldReturn:
[textField resignFirstResponder];
then your return YES;
try putting a NSNotification when keyboard is appeared and set the textfield as firstresponsder. The problem you are using might be because of the textfield is not the first responsder in your case.
How to put a UITextField inside of a UITableViewCell (grouped)? I want a user to be able to edit it.
Add the UITextField as an subview of the UITableViewCell's contentView:
[mycell.contentView addSubview:view];
Apple's own UICatalog demo application has an example of placing UITextFields in Grouped UITableView Cells: http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html
Check the contents of TextFieldController.m
Plus there's lots of other great code there for working with UIKit Objects.
This is how I've implemented it into my application, however you'll obviously need to change a few things. Hope this helps.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.
if ([indexPath section] == 0) {
tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tUser.adjustsFontSizeToFitWidth = YES;
tUser.textColor = [UIColor blackColor];
tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tPass.adjustsFontSizeToFitWidth = YES;
tPass.textColor = [UIColor blackColor];
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
tUser.placeholder = #"#JohnAppleseed";
tUser.keyboardType = UIKeyboardTypeEmailAddress;
tUser.returnKeyType = UIReturnKeyNext;
}
if ([indexPath row] == 1) {
tPass.placeholder = #"Required";
tPass.keyboardType = UIKeyboardTypeDefault;
tPass.returnKeyType = UIReturnKeyDone;
tPass.secureTextEntry = YES;
}
}
tUser.backgroundColor = [UIColor whiteColor];
tUser.autocorrectionType = UITextAutocorrectionTypeNo;
tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
tUser.textAlignment = UITextAlignmentLeft;
tPass.backgroundColor = [UIColor whiteColor];
tPass.autocorrectionType = UITextAutocorrectionTypeNo;
tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
tPass.textAlignment = UITextAlignmentLeft;
tUser.clearButtonMode = UITextFieldViewModeNever;
tPass.clearButtonMode = UITextFieldViewModeNever;
[tUser setEnabled:YES];
[tPass setEnabled:YES];
//[tUser release];
//[tPass release];
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = #"Username";
[cell addSubview:tUser];
[tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_name_preference"]];
}
else {
cell.textLabel.text = #"Password";
[cell addSubview:tPass];
[tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_pass_preference"]];
}
}
return cell; }
Hope it helps.