【发布时间】:2015-09-17 02:46:53
【问题描述】:
我有一个带有UITextField 的tableView 单元格来输入文本。
我正在使用以下代码填充 tableView:
- (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;
}
以下是永远不会执行的 textField 的 @selector(editingChanged:) 方法。我做错了什么?
-(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];
}
【问题讨论】:
-
顺便说一句 -
editingChanged:不是UITextFieldDelegate的方法。 -
看起来那里复制和粘贴的代码太多。该单元格创建方法发生了一些奇怪的事情。您正在实例化
IngredientCell并可能重新添加其中一个子视图?您还将子视图添加到单元格而不是其 contentView。该代码有太多错误,无法识别正在发生的事情。也许发布您的 IngredientCell 实现(这是使用情节提要中的原型单元创建的吗?)。我猜你在那个单元格中有 2 个文本字段,一个遮住了另一个。
标签: ios objective-c uitableview uitextfield