I have UITextField, i need to make it not to respond touches, because it is located on the UITableViewCell, and when user touches the textField, didSelectRowAtIndexPath is not getting called.
This textField has also clear button, i need to detect user taping on the clear button only, not in the rest textField area.
I tried, textField.userInteractionEnabled = NO. In this case clear button doesn't work of course, the same is with textField.enabled = NO;
UITextField *textField = [[UITextField alloc] initWithFrame:someFrame];
textField.textAlignment = NSTextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.delegate = self;
Help, please.
Thanks in advance.
In that case one work around can be used. You have to make a view that mask the textfield's text area but not clear button like this
We suppose that
someFrame = (20,50,100,44)
And textfield button around 20*20
So our view will be
UIView *maskView = [[UIView alloc] initWithFrame: CGRectMake (20,50,80,44)];
maskView.backgroundColor = [UIColor clearColor];
Add this view after your textfield added. And it will mask textfield's that area which used for text and leave button area as it is.
Remember, this is not tested and just a work around. So any other method is there than I recommend to you to use that. And change value according to your need.
Are you loading a cell from the nib or else. I have just tried to make my own custom cell like below
Assigning tag ( it is 9 in my sample ) for the uitextfield
In viewForCellAtIndex :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell ;
cell = [tableView dequeueReusableCellWithIdentifier:#"purchaseNibCell"];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"PurchaseNibCell" owner:self options:nil];
cell = self.purchaseCell;
self.purchaseCell = nil;
}
UITextField *text = (UITextField*)[cell viewWithTag:9];
text.enabled = NO; <------ if you have not done in the xib file.
cell.backgroundView = [[CustomCellBackground alloc] init];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"accessory.png"]];
cell.accessoryView = accessoryView;
return cell;
}
hope it helps
Related
I just started Xcode objective-c recently and right now I'm trying to create a tableview with textfields in them to type. Ive looked into other stack overflow questions but many are from 6-8 years ago and seem to have a wide rage of answers and extremely complex. Could someone help me with the basics of how I can insert a textfield in a table view and give me some advice. Thanks!
Write this code in uitableview cell
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.clearsOnBeginEditing = NO;
textField.textAlignment = UITextAlignmentRight;
textField.delegate = self;
[aCell.contentView addSubview:txt];
You can do this as follows:
Drag and drop a UITableView in to your view
Drag and drop a UITableViewCell into your table.
Drag and drop a UITextField (or any
other UI component that you need)
I would suggest you to please refer tutorials for that like
1.https://videos.raywenderlich.com/courses/22-table-views-in-ios/lessons/8
2.https://www.appcoda.com/expandable-table-view/
They have best tutorials with all steps you can easily do whatever you want.
I hope it will help you.
Thanks.
Try below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
/*
* Now that we have a cell we can configure it to display the data corresponding to
* this row/section
*/
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
tf.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf.backgroundColor=[UIColor whiteColor];
tf.text=#"Hello World";
[cell.contentView addSubview:tf];
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
You can create a custom cell. Add the textfield in it and use that in the table by registering the nib or class.
I have a simple tableView with 20 rows. I created a subclass custom UITableview cell, and in cellforRowAtIndex, I add a textfield to the cell once every other 3 rows. When I scroll up and down text fields show up in the wrong rows. Note, I can't make UItextfield part of my custom cell, because this can be anything, checkbox, radio button, but for simplicity I chose UITextfield... What am I doing wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TestCellIdentifier";
testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
else{
//HMMM I also tried removing it before adding it- it doesn't work neither
for(UIView *v in cell.subviews){
if(v.tag == 999 ){
[v removeFromSuperview];
}
}
//add UItextField to row if it's divisible by 3
if(indexPath.row %3 ==0){
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(400, 10, 300, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = [NSString stringWithFormat:#"%d",indexPath.row];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.tag = 999;
[cell addSubview:textField];
}
}
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
return cell;
}
don't use the Reusability ?
in this scene,i will not use the reusability
Reusing cells is a good thing and you should be able to do it.
You could consider removing the text field from the cell when it goes offscreen, before it is queued for reuse, in the delegate protocol method:
– tableView:didEndDisplayingCell:forRowAtIndexPath:
Knowing the row number you would know whether or not to remove the textfield.
Edited to add explanation:
On first glance your code looks OK, so I did a little test project.
The problem with your original code is that you're adding the textfield to the wrong "view" -- UITableViewCells have some structure that you need to pay attention to. Look at the documentation for the UITableViewCell contentView property. It says, in part:
If you want to customize cells by simply adding additional views, you
should add them to the content view so they will be positioned
appropriately as the cell transitions into and out of editing mode.
So the code should add to and enumerate subviews of the cell's contentView:
for(UIView *v in cell.contentView.subviews){
if(v.tag == 999 ){
[v removeFromSuperview];
}
}
...
textField.tag = 999;
[cell.contentView addSubview:textField];
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
My table view has inline cells adding.
I've got in cellForRowAtIndexPath:
VMEditableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"EditableCell"];
cell.editableTF.font = [UIFont fontWithName:#"Helvetica" size:17.0];
cell.editableTF.delegate = self;
cell.editableTF.tag = indexPath.row;
[cell.editableTF setBorderStyle:UITextBorderStyleNone];
if ([self.extrasArray[indexPath.row] isEqual:#0]) { // recognise new added cell
self.extrasArray[indexPath.row] = #"";
[cell.editableTF becomeFirstResponder];
}
cell.editableTF.text = self.extrasArray[indexPath.row];
return cell;
When i start table view - inline adding works fine.
The problem starts when i use "clear whole list" button - which simply removes all objects from extrasArray.
When I add cell after clearing it's being dequeued improperly and the textfield doesnt respond to becomeFirstResponder.
(it's not nil, it calls textFieldShouldBeginEditing(always YES) but nothing more happens - no keyboard is showing up.
My last idea was to to override prepareForReuse method inside of Cell Implementation - but sadly neither setting editableTF to nil and reinitalizing it doesnt work.
How can I force cell to recreate itself instead of coping?
Try to create a cell like this
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Set up the cell..
[self configureCell:cell atIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
'
So todays morning was very good for code soultions and finally I came up with one for this problem:
I got rid of storyboard for this cell and changed all pointers of custom cell to strong (so I own them).
Dequeque reusable cell was recreating UITextField from deleted rows making controller confused about ownership and becoming first responder.
be aware that creating them inside of cell init didn't do the trick.
TextField MUST be created and added as subview (also assigned to strong pointer so I get control over it later).
Here is some code:
VMEditableTableViewCell *cell = [[VMEditableTableViewCell alloc] init];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UIImage *bulletImage = [UIImage imageNamed:#"common_ico_dot.png"];
UIImageView *bulletImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,17,10, 10)];
bulletImageView.image = bulletImage;
UIImage *tickImage = [UIImage imageNamed:#"common_green_tick.png"];
UIImageView *tickImageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.bounds.size.width-30, 17, tickImage.size.width, tickImage.size.height)];
tickImageView.image = tickImage;
tickImageView.hidden = YES;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(60, 10, 227, 30)];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
textField.font = [UIFont fontWithName:#"Maven Pro" size:17.0];
textField.tag = indexPath.row;
[textField setBorderStyle:UITextBorderStyleNone];
[cell addSubview:bulletImageView];
cell.bulletImageView = bulletImageView;
[cell addSubview:tickImageView];
cell.tickImageView = tickImageView;
if (cell.editableTF == nil) {
[cell addSubview:textField];
cell.editableTF = textField;
}
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.