UITextField in UITableViewCell crash - ios

I have a text field on custom tableview cell in right half side. When I directly click on the text field am getting the keyboard. But when I clicked on the cell directly am getting the crash.
In table view didSelectRowAtIndexPath method, I have the code
[cell.txtField becomeFirstResponder];
In the console log am getting this below message:
no index path for table cell being reused
Reference crash here

IN your screen shot you are calling:
[cell.txvalue becomeFirstResponder];
Do you mean:
[cell.textField becomeFirstResponder];
?
I'm guessing cell.textValue is a string ?
As for the console log, are you calling setClearsOnInsertion property enabled on your textview ?

first of all you create configure cell as below
- (void)configureCellAcountSet:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
UITextField *txt_UserName = [[UITextField alloc] initWithFrame:CGRectMake(85, 105, 150, 25)];
txt_UserName.borderStyle = UITextBorderStyleRoundedRect;
txt_UserName.font = [UIFont systemFontOfSize:13];
txt_UserName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_UserName.keyboardType = UIKeyboardTypeDefault;
txt_UserName.returnKeyType = UIReturnKeyDone;
txt_UserName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_UserName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txt_UserName.delegate = self;
[[cell contentView] addSubview:txt_UserName];
}
After taht confige cell use in table delegete mehode. Dont do anything didSelectRowAtIndexPath.when you click on textfield it open keyboard and click on cell nothing done there.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"section0";
tbl_setting.layer.cornerRadius = 10.0;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureCellAcountSet:cell atIndexPath:indexPath];
}
You have to implement UITextField delegate.

Related

Textfield data hide when scroll tableview

I am developing IOS App. Multiple textfield add on tableviewcell.when enter data on texfield than scroll tableview textfield data hide. How to manage textfield on tableview.Please Help me and thanks in advance
code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableViews{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"cell";
UITableViewCell *cell;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
UITextField *textFieldName = [[UITextField alloc]initWithFrame:CGRectMake(8.0f, 3.0f, 80.0f, 36.0f)];
[cell addSubview:textFieldName];
[textFieldName setDelegate:self];
textFieldName.tag = indexPath.row+1;
UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(96.0f, 0.0f, 1.0f, 50.0f)];
line.backgroundColor = [UIColor colorWithRed:214.0/255.0 green:214.0/255.0 blue:214.0/255.0 alpha:1.0];
[cell addSubview:line];
[cell setSeparatorInset:UIEdgeInsetsZero];
tableView.allowsSelection=NO;
return cell;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([textField tag]-1) inSection:0];
UITableViewCell *Cell = [_dairyTable cellForRowAtIndexPath:indexPath];
}
How to deal with UITextField Value in UITableView :
First of all you have to create mutable Array with count equal to the number of UITextField.
In cellForRow do cell.textField.tag = indexPath.rowsothat you can identify each UItextField.
Save every UITextFieldvalue in the array in the same sequence with each character change. So, do code in textdidChange delegate method of UITextField by identifing the UITextfield by its tag as textfield.tag.
Whenever user enters or edit the text, you get that text in array.
Now in cellForRow add below line:
cell.textfiled.text = arrDataText[index.row]

TextLabel and UITextField as accessoryview are overlaping each other

I have a tableview cell in which I am adding a textfield as accessory view, but textlabel and the textfield in accessory view are overlaping each other as shown in the image. Strangest thing is that if I set the background color of the cell, it shows it over the whole cell but when I check its frame, It shows a width of 240.0. What am I missing here?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = #"Cell";
SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[SettingCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryNone;
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
cell.textLabel.text = #"Facebook default message with image";
UITextField* textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 500, 30)];
cell.accessoryView = textField;
textField.delegate = self;
textField.text = facebookDefaultMessageForImage;
textField.placeholder = #"Default message";
textField.tag = 1;
[textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
return cell;
}
You are missing constraints, please take a look how to work with constraints.

Adding UIDatePicker to cell content view

I'm attempting to put an instance of UIDatePicker in the accessory view of a UITableView cell, and have been following this SO thread as a template. However, it looks as if the picker is being placed above the cell entirely:
Below is the code I'm using to try to add a Date Picker to the accessory view of a UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellNewRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RowCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
switch (indexPath.row) {
case EmployeeOvertimeRow:
cell.textLabel.text = NSLocalizedString(#"Test", #"One");
_datePicker = [[UIDatePicker alloc]init];
_datePicker.datePickerMode = UIDatePickerModeTime;
//OLD: cell.accessoryView = _datePicker;
//POST EDIT
[cell.contentView addSubview:_datePicker];
break;
default:
break;
}
return cell;
}
Does anyone have any guidance on what I'm doing wrong or how to fix this?
The accessoryView of a UITableViewCell is surely not what you think : it's the little view at the right of the cell, typically, it's an arrow, and it's pretty small, not made to be the width of the screen at all. You should try adding your view to the contentView. You will need to set a bigger height for your cell in the heightForRowAtIndexPath method, too.

Add a UITextField that retains its text in a UITableViewCell

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;
}

ios MasterDetail app: UITextField in tableview not work

I have a master detail app in ios, with SDK 7.0, Xcode 5, using ARC.
master has many items, detail has a table view. When I click on an item, the contents of tableview will change. This works well until I put a UITextField in each cell, because I want to edit the contents in the table.
The problem is: when I click on a new item, the old contents don't disappear,so the contents of a cell is a superposition of the new UITextField's text and the old UITextField's text.
The first normal tableview like this:
After I click on an new item, the tableview will like this:
The snippet of codes of master is:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LHBPoetry *poetry = poetryArray[indexPath.row];
self.detailViewController.poetryId = poetry.poetryId;
}
I have tried a lot of things in the above method, for example, I make all instances of the detail view controller to be nil; table view's data array removeAllObejects; table view reloadData; It can't fix the problem.
The snippet of detail is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"detailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITextField *textField = textField = [[UITextField alloc] initWithFrame:
CGRectMake(90, 12, 200, 25)];
textField.tag = indexPath.row;
textField.text =_sentenceArray[indexPath.row];
textField.clearsOnBeginEditing = NO;
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:textField];
textField.text = _sentenceArray[indexPath.row];
return cell;
}
I draw this tableview in Main.storyborad, It has a prototype cell with an identifier.
Any help will be appreciated.
k there is something i want to tell, wy because u are keep on adding the textfields for reused cells, there is not one textfield in the cell ..:) there are more then one text field's, because of that u are getting overlapped with one other, i think u are using default "master- detail" application, and modifying it..:)
oky for that u need to modify like below
in master controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(2, 3, 300, 30)];
[textField addTarget:self action:#selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEnd]; //hear u are adding once initially
textField.tag = 100;
[cell addSubview:textField];
}
NSString *object = _objects[indexPath.row];//_objects is mutable array holding the sentences or names
UITextField *textField = (UITextField *)[cell viewWithTag:100];//after that u are reusing the textfields
textField.text = object;
textField.tag = indexPath.row;
return cell;
}
now you are creating the cell thats wy u dont want the prototype cell remove it from story board
in the above u removed the custom cell becz u are creating the cell in the code it self
now in the method
- (void) textFieldDone:(UITextField *)inTextFIeld
{
int index = inTextFIeld.tag;
[_objects replaceObjectAtIndex:index withObject:[inTextFIeld text]];
[self.masterTableVIew reloadData];//made connection to ur tableview
}

Resources