i´m adding a UITextField on a Cell.
- (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];
}
myLoc = [[UITextField alloc] initWithFrame:CGRectMake(35, 10, 250, 40)];
myLoc.adjustsFontSizeToFitWidth = YES;
myLoc.textColor = [UIColor blackColor];
myLoc.textAlignment = UITextAlignmentCenter;
myLoc.placeholder = #"Enter Location";
myLoc.returnKeyType = UIReturnKeyDone;
myLoc.autocorrectionType = UITextAutocapitalizationTypeNone;
myLoc.tag = indexPath.row;
myLoc.delegate = self;
[myLoc setEnabled:YES];
[cell addSubview:myLoc];
return cell;
}
and in the textFieldShouldReturn i would write the text from the textfield in a mutable array, and store in nsuserdefaults.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[myLoc resignFirstResponder];
[locationArray addObject:textField.text];
locName = [NSUserDefaults standardUserDefaults];
[locName setObject:locationArray forKey:#"textName"];
[locName synchronize];
NSLog(#"Done pressed %#",myLoc.text);
return YES;
}
...but the myLoc.text is always null
any ideas for me?
In the delegate method, don't use myLoc to reference the textField, but use the actual textField pointer provided as parameter of the function. This one should actually point to the correct textfield and thus textField.text should have the correct value.
On a side note: Where do you define myLoc? It looks like you're trying to set a property on your viewcontroller. This way you'll always overwrite the property. For this you don't need a property at all, so just define myLoc locally in the function scope like UILabel *myLoc
You're better off creating your own custom UITableViewCell. You can see how to create one here:
tutorial
Trying to tack on to the existing UITableViewCell is difficult at best and impossible at worst. You're better off with custom cells.
Your text should never be NULL, since its an NSString. Are you sure you're talking about the text from the text field?
What outputs when you add NSLog(#%"#", textField.text); to your shouldReturn method?
Are you sure that you have typed in the text field? Also, even without typing, you should be getting #"" instead of nil.
Related
In my app rows are added to my TableView from a different view. When the user adds the rows the user is taken back to the TableView. The problem is that the text that was previously entered is no longer shown.
I am able to load it with an NSMutableDictionary but the user cannot see it. Any ideas on what I should do? what code I should add and where I should add it? Thanks a lot!
Here is code from a tableview method. I think the fix will go in here somewhere.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.wtf = [[UITextField alloc]init];
NSUInteger count =0;
for (NSMutableDictionary *search in dataForAllRows){ //this just helps me pull the right data out of an array of NSMutableDictionary's
if ([search valueForKey:#"indexSection"] == [NSNumber numberWithInteger:(indexPath.section -1)]) {
if ([search valueForKey:#"indexRow"] == [NSNumber numberWithInteger:indexPath.row]) {
NSMutableDictionary *match = [dataForAllRows objectAtIndex:count];
[cell.wtf setText:[match objectForKey:#"wtf"]];
NSLog(#"%#",cell.wtf.text); // this outputs the correct value in the command line
}
}
count++;
}
}
}
Here is the code for my CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize wtf, cellPath;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)layoutSubviews{
wtf = [[UITextField alloc] initWithFrame:CGRectMake(7, 3, 65, self.contentView.bounds.size.height-6)];
self.wtf.delegate = self;
[wtf setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[wtf setAutocorrectionType:UITextAutocorrectionTypeNo];
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[wtf setBorderStyle:UITextBorderStyleRoundedRect];
wtf.textAlignment = NSTextAlignmentCenter;
wtf.keyboardType = UIKeyboardTypeNumberPad; //
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[wtf setPlaceholder:#"enter"];
[self.contentView addSubview:wtf];
}
Consider defining the cell with identifier #"Cell" in IB as a prototype row of the table. Then, use dequeueReusableCellWithIdentifier:forIndexPath: to retrieve the cell in cellForRowAtIndexPath. It's easier to understand what your cells will look like, and you can avoid some mistakes that are common when defining subviews in code.
Speaking of common mistakes, your code appears to present a couple: it doesn't frame the text field, nor does it add it as a subview of the cell. Both would explain not seeing the text field.
#williamb's advice is correct and necessary: only build the cell's subview's if they are absent, but the building of the cell is incomplete...
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UITextField *wtf = [[UITextField alloc]initWithFrame:CGRectMake(10,10,200,42];
[wtf setDelegate:self];
[cell addSubview:wtf];
cell.wtf = wtf;
}
As I mentioned in comment, a sectioned table ought to be supported by a 2D array. The outer array is an array of sections. Each section array is an array of dictionaries equal to the ones you're searching each time through this method, but pre-arranged so all that's done in cellForRowAtIndexPath is indexing into an array:
NSDictionary *d = self.myCorrectlyStructuredModel[indexPath.section][indexPath.row];
cell.wtf.text = d[#"wtf"];
It's not a big challenge to build this from what you have. Consider doing this right after you solve the text field problem. I (or others) can give you some advice -- if you need any -- about how to build that structure.
It looks like you are only setting the text value of your textfield if that cell does not exist and overriding your textfield instance to one that does not have a frame as #danh mentioned. What I believe you want to do is reuse the textfield once it is added to your cell's contentview and change what that textfield shows for each index path.
Try refactoring your cell code to be more like:
#implementation ExerciseCell
#pragma mark - Init
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self)
{
wtf = [[UITextField alloc] initWithFrame:CGRectMake(7, 3, 65, 44)];
wtf.delegate = self;
[wtf setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[wtf setAutocorrectionType:UITextAutocorrectionTypeNo];
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[wtf setBorderStyle:UITextBorderStyleRoundedRect];
wtf.textAlignment = NSTextAlignmentCenter;
wtf.keyboardType = UIKeyboardTypeNumberPad;
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[wtf setPlaceholder:#"enter"];
[self.contentView addSubview:wtf];
}
return self;
}
and your tableview datasource class to be more like
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.wtf setDelegate:self];
}
NSUInteger count = 0;
for (NSMutableDictionary *search in dataForAllRows){ //this just helps me pull the right data out of an array of NSMutableDictionary's
if ([search valueForKey:#"indexSection"] == [NSNumber numberWithInteger:(indexPath.section -1)]) {
if ([search valueForKey:#"indexRow"] == [NSNumber numberWithInteger:indexPath.row]) {
NSMutableDictionary *match = [dataForAllRows objectAtIndex:count];
[cell.wtf setText:[match objectForKey:#"wtf"]];
NSLog(#"%#",cell.wtf.text); // this outputs the correct value in the command line
}
}
count++;
}
}
}
Also do you mean to assign the the textField's delegate twice? Once in the cell and once in the tableview's datasource?
In order to load text into the UITextField in the CustomCell I added the following method
CustomCell.m
-(void)viewMyCellData{
//here I can set text to my textfield
wtf.text = #"Desired Text"; //this will read in every wtf textfield in the table
//getting the right text from an array will be asked in another question that I will post
//in a comment to this answer
}
Next we call this using [self viewMyCellData]
at the end of our
-(void)layoutSubviews method which is also in CustomCell.m
How do I achieve a UITableViewCell with a textField in it? Ive been through a few similar questions on StackOverflow, but I find most of them deal with cases where the tableView's display is always constant and the know exactly what values go where. For my implementation the number of section and number of rows per section are determined by the user and could be anything. I have been able to get a textField in the cell but I find any text entered is lost when I scroll the table and the cell disappears from view. This is what i have so far
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
UITextField *myTextField = [[UITextField alloc]
initWithFrame:CGRectMake(215, (cell.contentView.bounds.size.height-30)/2, 60, 30)];
[myTextField setDelegate: self];
myTextField.tag = indexPath.section;
NSLog(#"%ld", (long)myTextField.tag);
myTextField.placeholder = #"0%";
myTextField.borderStyle = UITextBorderStyleRoundedRect;
[cell addSubview:myTextField];
cell.textLabel.text = self.cellLabels[indexPath.section];
return cell;
}
I know what the problem is, I just cant figure out a work around. Even if I store everything the user enters in each textField in an array somehow, I cant set the text for each field when it return from being off screen as the cellForRowAtIndexPath gets called again and the textField is redrawn.
This is pretty much what I want, functionality wise. But "Paper1" and "Paper2" may be different sections...
Use a view model. Store the text for each cell in an array and then set the text in cellForRowAtIndexpath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
UITextField *myTextField = [[UITextField alloc]
initWithFrame:CGRectMake(215, (cell.contentView.bounds.size.height-30)/2, 60, 30)];
[myTextField setDelegate: self];
myTextField.tag = indexPath.section;
NSLog(#"%ld", (long)myTextField.tag);
myTextField.placeholder = #"0%";
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.text = [self textForCellAtIndexPath:indexPath];
[cell addSubview:myTextField];
cell.textLabel.text = self.cellLabels[indexPath.section];
return cell;
}
Im getting a crash, when using a UItextField, inside my customCell, and when i resignFirstResponder the textfield, but its not visible anymore(the table view scrolled out of window). I still can find the textfield, the pointer continues accessible, it is no null, and the crash only occurs on IOS7, on IOS6 i dont have this problem. Heres some code :
The textField is a global variable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * CellIdentifier = [NSString stringWithFormat:#"Cell%d",indexPath.row];
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableCell alloc] init];
if(indexPath.row == 0)
{
[textField setFrame:CGRectMake(15, 5, cell.frame.size.width-60, cell.frame.size.height)];
textField.textAlignment = NSTextAlignmentLeft;
[textField setBorderStyle:UITextBorderStyleNone];
textField.textColor = [UIColor blackColor];
textField.tag = indexPath.row;
textField.delegate = self;
textField.secureTextEntry = YES;
[textField setFont:[UIFont fontWithName:#"Arial-BoldMT" size:15]];
textField.textColor = [UIColor whiteColor];
textField.returnKeyType = UIReturnKeyDone;
[textField setAdjustsFontSizeToFitWidth:YES];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Senha" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[cell.contentView textField];
}
}
return cell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
// NSLog(#"text field %#",textField);
// NSLog(#"tfield return: %d",textField.isFirstResponder);
[textField resignFirstResponder];
// [self.view endEditing:YES];
return NO;
}
I've successfully fixed a similar crash bug with the help of Apple. The key is the reuseIdentifer.
The quote is from a mail from Vincent Gable of Apple Developer Technical Support:
This is a known behavior change that happens in iOS 7 with UITableView, when cells are not reused.
The fix here is to make sure that you follow proper cell reuse. If you do not want to re-use UITableViewCells, then it is recommended that you simply layout all your views inside a UIScrollView.
To make sure cells are re-used, make sure you are passing the same string to dequeueReusableCellWithIdentifier: that you pass to reuseIdentifier: when using alloc/init to make the cell. This string can not be nil.
So I think you should make sure you've set TableCell's reuseIdentifer property with the same value you've passed to dequeueReusableCellWithIdentifier:
You need to do some more research into how UITableViews work and reconsider your design. Storing a UITextField in a global variable and trying to position it like this is not the right approach. Even if you could solve the immediate problem, which is likely that the UITextField has been released along with the UITableViewCell, this design is only going to get you into trouble further down the line.
Instead, consider subclassing UITableViewCell and adding a UITextField property to your subclass.
You probably don't want to be using a different CellIdentifier for every single row either.
Maybe i've solved.
It's a little bit dirty methot but i think it work.
I store all the cell that cellForRowAtIndexPath create
if (!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:#"FormCell_%#",cellID] owner:nil options:nil] lastObject];
[self.allTheCell addObject:cell];
}
the app doesn't crash anymore on ios7
I have seen every post that is close to this question, and still not finding something useful. I have textFields in every cell that is being used as a form for the user to fill out. Everything with the cells works fine except when scrolling, the input in the textFields disappears when the cell scrolls off screen. I know this is because of dequeue. But there should be a way to save the data entered so that it doesn't disappear when scrolling or exiting the app. I also want to be able to take this info and email it as a PDF, or document. What is the best way to achieve this? The code below is an example of how I am generating my cells etc.
.h file
#interface MasterViewController : UITableViewController <UITextFieldDelegate, UITextFieldDelegate, UITableViewDataSource, UINavigationBarDelegate>{
NSString* name_;
UITextField* nameFieldTextField;
}
// Creates a textfield with the specified text and placeholder text
-(UITextField*) makeTextField: (NSString*)text
placeholder: (NSString*)placeholder;
// Handles UIControlEventEditingDidEndOnExit
- (IBAction)textFieldFinished:(id)sender;
#property (nonatomic,copy) NSString* name;
.m file
#synthesize name = name_;
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.name = #"";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// Make cell unselectable and set font.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:#"ArialMT" size:13];
if (indexPath.section == 0) {
UITextField* tf = nil;
switch ( indexPath.row ) {
case 0: {
cell.textLabel.text = #"Name" ;
tf = nameFieldTextField = [self makeTextField:self.name placeholder:#"John Appleseed"];
nameFieldTextField.tag = 1;
[cell addSubview:nameFieldTextField];
break ;
}
// Textfield dimensions
tf.frame = CGRectMake(120, 12, 170, 30);
// Workaround to dismiss keyboard when Done/Return is tapped
[tf addTarget:self action:#selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
}
return cell;
}
// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {
//Section 1.
if ( textField == nameFieldTextField ) {
self.name = textField.text ;
}
}
- (void)viewWillAppear:(BOOL)animated{
NSString *nameCellString = [[NSUserDefaults standardUserDefaults] stringForKey:#"nameCellString"];
nameFieldTextField.text = nameCellString;
}
- (void)viewWillDisappear:(BOOL)animated{
NSString *nameCellString = self.name;
[[NSUserDefaults standardUserDefaults] setObject:nameCellString forKey:#"nameCellString"];
}
There are actually two problems here, both of them being in your cellForRowAtIndexPath: implementation.
You are putting the text field into the cell, even if this cell is reused and already has a text field. Thus you are actually piling text field over text field, covering up the previously existing text field.
You are not putting the text back into the text field if there was already text in the text field for that row (index path).
In other words, the cells are (as you rightly say) reused, so it is up to you to take that fact into account. You must look at the state of the incoming cell, and reconfigure the cell accordingly.
First off, I urge you to consider creating a custom cell in a storyboard, and grabbing that. It's a lot easier than coding one, and I think it's the future. That said, look into populating your tableViews with NSArrays, instead of hard-coding strings into the cellForRowAtIndexPath method. I've taken the liberty of giving you an example of this.
The following is based on your code, and should be a copy/paste solution. Look it over, and see how it operates.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *titlesArray = #[#"Name", #"Birthday", #"Favorite Food"];
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"%i%i", indexPath.section, indexPath.row]];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// Make cell unselectable and set font.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:#"ArialMT" size:13];
// Populate label from array
cell.textLabel.text = titlesArray[indexPath.row];
if (indexPath.section == 0) {
UITextField* tf = nil;
switch ( indexPath.row ) {
case 0: {
tf = nameFieldTextField = [self makeTextField:self.name placeholder:#"John Appleseed"];
nameFieldTextField.tag = 1;
[cell addSubview:nameFieldTextField];
break ;
}
// Textfield dimensions
tf.frame = CGRectMake(120, 12, 170, 30);
// Workaround to dismiss keyboard when Done/Return is tapped
[tf addTarget:self action:#selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
}
// Set the reuse identifier to a unique string, based on placement in table
// This ensures that the textField will retain its text
cell.reuseIdentifier = [NSString stringWithFormat:#"%i%i", indexPath.section, indexPath.row];
}
return cell;
}
// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {
//Section 1.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
switch (textField.tag) {
case 1:
[defaults setObject:textField.text forKey:#"nameCellString"];
self.name = textField.text;
break;
default:
break;
}
[defaults synchronize];
}
EDIT: Changed to accommodate more cells.
You should use array of UIDictionary for your tableDataSourceArray Like:
step1)
NSArray *tableDataSourceArray = [[NSArray alloc]init];
NSMutableDictionary *cellData = [[NSMutableDictionary alloc]init];
[cellData setValue:#"" forKey:#"Name"];
...//so on
[tableDataSourceArray addObject:cellData];
cellData = nil;
repeat step1 as number of records you have.
Now in cellForRowAtIndexPath:
nameFieldTextField.tag = indexPath.row; //To store index of dataSourceArray
nameFieldTextField.text = [[tableDataSourceArray objectAtIndex:indexPath.row] valueForKey:#"Name"];
And at last in textFieldDidEndEditing:
NSMutableDictionary *cellDataDic = tableDataSourceArray objectAtIndex:textField.tag];
[cellDataDic setValue:textField.text forKey:#"Name"];
hope it will help you.
I think the easiest way to fix your problem is to create a new class for your cell (inherit from UITableViewCell) and add new property like customerTextField (UITextField). In constructor add new textfield but with CGRectZero. In method layoutSubviews you will assign CGRect for your textfield.
Generally speaking this approach will make your UIViewController cleaner (you will reduce number of checks for textfield state).
So I'm trying to figure out ideas for my registration page in xCode -- something like this: http://weswilliams.me/wp-content/uploads/2011/06/IMG_2525-e1307910945329.png
At any rate, I can't figure out what objects they are using to achieve this display. It looks like a TextField on top of a Button? If it is, I can never get the Text Field to sit on top, it always falls behind the button, thus making it invisible.
Any tips or suggestions?
This is not a textfield on the button. Actually it is text box inside a table view. You have to do the following :
Take a table view on the nib.
Create the outlet and set the delegate and datasource.
Then add the following code to your .m file.
try this one
before this set the number of rows the table view has.
- (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;
}
Or you can see this link See this answer on SO
Thats a basic grouped UITableView. Read up on Apple docs. There are a ton of tutorials on that too.