I have a tableView cell with a UITextField
to enter the text. I am populating the tableView with the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Ingredient Cell";
IngredientsTableViewCell *ingredientCell = [self.ingredientsTableView dequeueReusableCellWithIdentifier:cellIdentifier];
// NSManagedObjectContext *managedObject = [self.ingredientItems objectAtIndex:indexPath.row];
if (ingredientCell == nil)
{
ingredientCell = [[IngredientsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ingredientCell.accessoryType = UITableViewCellAccessoryNone;
[ingredientCell addSubview:ingredientCell.ingredientTextField];
[ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
}
//Populate the textfield in the ingredientCell
ingredientCell.ingredientTextField.text = [self.ingredientItems objectAtIndex:indexPath.row];
return ingredientCell;
}
Following is the @selector(editingChanged:)
method for the textField which never executes. What am I doing wrong?
-(void) editingChanged:(id)sender{
NSLog(@"hi");
// get the text being entered
NSString *ingredientText = ((UITextField *)sender).text;
//get the index of the selected row
NSInteger selectedIndex = [self.ingredientsTableView indexPathForSelectedRow].row;
//save the text to the array
[self.ingredientItems setObject:ingredientText atIndexedSubscript:selectedIndex];
}
So you'll want to use the textFieldDelegate (or if you're adventurous you could use ReactiveCocoa).
Add this at the top
<UITextFieldDelegate>
And this in your cellForRowAtIndexPath
ingredientCell.ingredientTextField.delegate = self;
ingredientCell.ingredientTextField.tag = 1;
And this delegate method
//Use this method instead of addTarget:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField.tag == 1){
//do some stuff
}
}
see this stack overflow post: iOS - Adding Target/Action for UITextField Inside Custom UITableViewCell
But you might have a slightly different problem as well. This line of code seems redundant:
[ingredientCell addSubview:ingredientCell.ingredientTextField];
You're ingredient cell seems like it should already have the ingredientTextField as a subview. This might cause you problems as well. You can add it as a subview in a xib that's attached to your ingredientsTableViewCell class, or simply add it in code as
[self addSubview:ingredientTextField]
Hope that helps.
if (ingredientCell == nil)
{
ingredientCell = [[IngredientsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ingredientCell.accessoryType = UITableViewCellAccessoryNone;
[ingredientCell addSubview:ingredientCell.ingredientTextField];
[ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
}
this portion looks much of problems try taking out this line from if statement
[ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
This will help you.
You can add this line in cellForRowAtIndexPath
and add UITextFieldDelegate
in Viewcontroller.h file
ingredientCell.ingredientTextField.delegate = self;
[ingredientCell.ingredientTextField addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
This is textfield controller event
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.