I'm building an app which contains user details view. i have used table view to keep 3 textfield. what i want to do is, when user clicks edit button, 1st textfield(phone Number TF) should not allow user to enter data & in remaining 2 textfield should allow user enter data. thanks in advance
Set the userInteractionEnabled property:
//UITextField *textfield;
textfield.userInteractionEnabled = NO; // TO stop UserInteraction and Yes TO enable User Interaction
do like
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"XXXXX"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"SimpleTableCell"];
}
cell.yourTextfield.userInteractionEnabled = YES;
if (indexPath.row == 0) // it disable your interaction in first cell
{
cell.yourTextfield.userInteractionEnabled = NO;
}
return cell;
}
Choice-2
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"XXXXX"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"SimpleTableCell"];
}
cell.yourTextfield.tag = indexPath.row; // set tag for each textfield
return cell;
}
on that delegate fire disable the interaction
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
BOOL editable;
if (textField.tag == 0) {
editable = NO;
} else if (textField.tag == 1 || textField.tag == 2) {
editable = YES;
}
return editable;
}
You don't need a table view to present three textfields. Basically, tableview's used for present big amount of data.
But you could simply set userInteractionEnabled to NO to prevent changing textfield.
Otherwise, if you still want to use tableview you still can disable user interaction in tableView:cellForRowAtIndexPath:
Related
I have a UITextField in UITableViewCell the text property is not updating.
My tableview delegate:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
KAPInputTableViewCell *cell = [self.registerTableView
dequeueReusableCellWithIdentifier:KAPInputCellIdentifier];
if (!cell) {
cell = [[KAPInputTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:KAPInputCellIdentifier];
}
switch (indexPath.row) {
case 0:
cell.inputTextField.placeholder = KAPLocalizedString(#"email", #"E-mail address");
cell.inputTextField.text = #"Test";
cell.tag = 0;
cell.errorLabel.text = self.emailError;
self.emailCell = cell;
self.emailTextField = cell.inputTextField;
break;
}
cell.inputTextField.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Then when I call,
NSLog(#"%#", self.emailTextField.text);
Later in my ViewController on the press of a button the edited text still returns "Test" while I changed the input value.
Please help.
I think you should just implement the UITextField delegate method:
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"%#", textField.text);
}
Here you will see your textField updated.
I have created a custom cell with a textField that initially is hidden.
When I choose the cell, the textField appears and I have to press on textField to write.
This I want is when I choose the cell to write immediately in textField. This is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ( indexPath.row == 0 ){
commentTextField.text = [NSString stringWithFormat:commentDetailTextLabel.text];
commentTextField.textColor = [UIColor yellowColor];
commentTextField.hidden = NO;
commentDetailTextLabel.hidden = YES;
}
}
Add [commentTextField becomeFirstResponder]; in your if body.
I am trying to call selectRowAtIndexPath on a UITableView that is a subview to the UIViewController I am calling it from.
I have set it up so that when you select a cell it goes grey and thats fine, however I am loading different data sets in and out of the UITableView and when ever a selection is made I am sending that selected NSIndexPath back to the UIViewController. Then when the view is next loaded with the correct data set for the NSIndexPath I call this method from my UIViewController.
if (codeIndexPath != nil) {
[filterViewController.tableView selectRowAtIndexPath:codeIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
Then in the UITableView class my cellForRowAtIndexPath and didSelectRowAtIndexPath look like this.
- (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];
}
// Configure the cell...
NSString *projectDescriptionString = [currentFilterMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = projectDescriptionString;
if (indexPath == currentlySelectedIndex) {
cell.highlighted = YES;
} else if (indexPath == currentlySelectedIndex) {
cell.highlighted = NO;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// send this data back in the delegate so you can use it to show where the tick is again if you need too.
currentlySelectedIndex = indexPath;
[[self delegate] updateInstallTableWithFilter:[currentFilterMutableArray objectAtIndex:indexPath.row] FilterType:filterType InstallIndex:indexPath];
}
When It loads on the screen the correct cell will highlight for a second then go back to white.
Update
New if statment inside cellForRowAtIndexPath
if ([indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = YES;
} else if (![indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = NO;
}
I am still receiving the same error.
UITableViewController has a property called clearsSelectionOnViewWillAppear. From the doc:
When the table view is about to appear the first time it’s loaded, the
table-view controller reloads the table view’s data. It also clears
its selection (with or without animation, depending on the request)
every time the table view is displayed. The UITableViewController
class implements this in the superclass method viewWillAppear:. You
can disable this behavior by changing the value in the
clearsSelectionOnViewWillAppear property.
So in that table view controller subclass, in viewDidLoad...
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
I have UITableViewController with a static table view and 2 sections. The first section has two cells, with a layout Table View Cell > Content View > Text Field.
The second section of table view cells has a layout that is Table View Cell > Content View > Label - and the cells expand when they're selected by using beginUpdates and endUpdates and reporting cell heights via tableView:heightForRowAtIndexPath:.
Right now, when I touch one of the first sections' cells, the keyboard comes up and the cursor is placed in the text field, but the cell is not selected - which means that if I had previously selected one of the cells in the second section, it would remain selected (and therefore expanded).
Also, if I have selected a cell in the first section with the text field given first responder, then I select a cell in the second section, the cell expands, but the first cell does not resign first responder and dismiss the keyboard.
Did I layout my table view incorrectly such that the cell selected states are not managed automatically and if not: what is the proper way to manage the selected states of the UITableViewCells?
I dont know how you have written the code. But from above information I have created one sample application. The code is as below and it is working as you want.
I have considered the table with 2 section.
First section has the textfield on Cell.
Second section has the UIlabel on the cell.
txtFieldCopy is of type UITextField declared in .h of class.
Please check the way I have assigned the Tag to the view as we need it to get the indexpath.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellStyle style =UITableViewCellStyleSubtitle;
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
int iTag=[indexPath row]+1;
txtFieldCopy=nil;
if(indexPath.section==1)
{
UILabel *lblFirst=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier];
lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 100,
40)] ;
lblFirst.tag=iTag;
[cell.contentView addSubview:lblFirst];
lblFirst=nil;
}
lblFirst = (UILabel *)[cell.contentView viewWithTag:iTag];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
lblFirst.text =#"Label 1";
}
else
{
UITextField *txtFirst=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier];
txtFirst = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 50,
40)] ;
txtFirst.tag=iTag;
txtFirst.delegate=(id)self;
[cell.contentView addSubview:txtFirst];
txtFirst=nil;
}
txtFirst = (UITextField *)[cell.contentView viewWithTag:iTag];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
txtFirst.text =#"text 1";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(txtFieldCopy)
{
[txtFieldCopy resignFirstResponder];
txtFieldCopy=nil;
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%d",textField.tag);
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:(textField.tag-1) inSection:0];
[tblSample selectRowAtIndexPath:iRowId animated:NO scrollPosition:UITableViewScrollPositionMiddle];
txtFieldCopy=textField;
}
Make sure if suppose in between you are reloading the table view then txtFieldCopy=nil; in the cellForRowAtIndexPath get called and set to null. so you can ignore this line. But before please check whether you are calling it again and again.
Let me know if it worked for you or not. I am sure it definitely work for you
So I'm trying to store a cell's indexpath when my custom cell is created in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I define a custom cell in class called "custom cell", and I added a property to that class defined as such:
#property (nonatomic, strong) NSIndexPath *indexPath;
in cellForRowAtIndexPath I set the index path with customCell.indexPath = indexPath.
When I NSLog it on the very next line it returns null. Could somebody please explain why this is happening?
Edit
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EditableCell";
EditableCell *editableCell = (EditableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (editableCell == nil) {
editableCell = [[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_myTextField = [editableCell textFieldCell];
if (indexPath.section == 0){
[_myTextField setPlaceholder:#"text"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else if (indexPath.section == 1){
[_myTextField setPlaceholder:#"text"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else {
[_myTextField setPlaceholder:#"text"];
[_myTextField setReturnKeyType:UIReturnKeyDone];
}
_myTextField.keyboardType = UIKeyboardTypeDefault;
_myTextField.delegate = self;
return editableCell;
}
Where I call [self.tableview indexpathforcell:cell]
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.returnKeyType == UIReturnKeyNext) {
UITableViewCell* myCell = (UITableViewCell*)textField.superview;
EditableCell *currentCell = (EditableCell *)myCell;
NSLog(#"INDEXPATH OF CURRENT CELL: %#",[self.tableView indexPathForCell:currentCell]);
}
}
You can determine the indexPath of a given cell using
-[UITableView indexPathForCell:]
If the cell needs to know it's own indexPath:
NSIndexPath * myIndexPath = [(UITableView *)self.superview indexPathForCell:self]
But if it does, something's not right!
Looking at your edited post I see the problem. You need:
UITableViewCell* myCell = (UITableViewCell*)textField.superview.superview
The textField is actually a subview of the cell's contentView. This always seems a bit wrong to me, so another way to handle this is to make your custom cell the UITextFieldDelegate, and then create a delegate protocol for your cell. So within the cell you'd have:
(BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.returnKeyType == UIReturnKeyNext) {
[self.delegate customCell: self textFieldDidReturn: textField];
}
return NO;
}
Then in your cell's delegate you'll have a reference to the cell and textField