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.
Related
how can i return keyboard on textfield while i have put the textfield on cell.contentview , i am using textfieldshouldreturn methodbut its not working So please tell me how can i solve it ?
this is my code for set textfield on tableview cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentitifier = #"cell";
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentitifier];
if (cell ==nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellidentitifier];
}
else
{
[cell prepareForReuse];
}
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 70, 70)];
imgview.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:imgview];
UITextField *_nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 10, 160, 22)];
_nameTF.placeholder =#"Client Name";
_nameTF.layer.borderWidth = 1.0;
[cell.contentView addSubview:_nameTF];
_nameTF = nil;
UITextField *_timeTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 37, 160, 22)];
_timeTF.placeholder = #"Time";
_timeTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_timeTF];
_timeTF = nil;
UITextField *_PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 64, 160, 22)];
_PhoneTF.placeholder = #"Phone";
_PhoneTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_PhoneTF];
_PhoneTF = nil;
return cell;
}
set delegates to textfield in cellForRowAtIndexPath as below, before return cell statement.
_nameTF.delegate = self;
_timeTF.delegate = self;
_PhoneTF.delegate = self;
and
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
and to your viewcontroller set delegate as below in .h file
#interface myviewVC : UIViewController<UITextFieldDelegate>
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.
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.
In the .h
#interface WordListTableController : UITableViewController <UITextFieldDelegate>
In the .m
// Customize the appearance of table view cells.
- (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...
UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
FirstField.delegate = self;
FirstField.tag = indexPath.row;
[cell.contentView addSubview:FirstField];
FirstField.returnKeyType = UIReturnKeyNext;
[FirstField release];
return cell;
}
// Handle any actions, after the return/next/done button is pressed
- (BOOL)textfieldShouldReturn:(UITextField *)textfield
{
[textfield resignFirstResponder];
return YES;
}
What am I missing? Breakpoint is not being visited?
The way you are adding the UITextField is problematic. You add it each time cellForRowAtIndexPath is called without ever removing it. So you might actually end up with several text fields stacked on top of each other.
Try moving FirstField's creation in the if (cell == nil) {} block. Maybe this will also solve your problem.
you do not need repeat create it, just create it while the cell was created!
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
FirstField.delegate = self;
FirstField.tag = indexPath.row;
[cell.contentView addSubview:FirstField];
FirstField.returnKeyType = UIReturnKeyNext;
[FirstField release];
}
Use this code snippet. textfieldShouldReturn is wrong.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I want to know how to make text field in tableview in iphone.
it means how to add uitextfield on uiTableviewCell and How handle it?
In order to handle means respond to it delegate and fetching value at the time of submission....
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,320,50)];
textField.tag = 1000;
[cell.contentView addSubview:textField];
[textField release];
In customization part,
UITextField *textField = [[cell contentView] viewWithTag:1000];
textField.text = #"Your text";
Tryout this code in CellForRowAtIndexPath:
UITextField *textField = [UITextField alloc] initWithFrame:CGRectMake(0,0,50,50)];
cell.contentView =textField;
[textField release];
Find the below link
Having a UITextField in a UITableViewCell
or try this one
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease];
cell.textLabel.text = [[NSArray arrayWithObjects:#"First",#"Second",#"Third",#"Forth",#"Fifth",#"Sixth",#"Seventh",#"Eighth",#"Nineth",#"Tenth",nil]
objectAtIndex:indexPath.row];
if (indexPath.row % 2) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
textField.placeholder = #"Enter Text";
textField.text = [inputTexts objectAtIndex:indexPath.row/2];
textField.tag = indexPath.row/2;
textField.delegate = self;
cell.accessoryView = textField;
[textField release];
} else
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Create a instance of either UITextView Or UTextField and add it into the UITableViewCell as sub view.