未在具有自定义文本字段的自定义 TableView 中设置第一响应者
Posted
技术标签:
【中文标题】未在具有自定义文本字段的自定义 TableView 中设置第一响应者【英文标题】:First responder is not being set in custom TableView having custom textfield 【发布时间】:2013-06-05 06:57:16 【问题描述】:我有一个包含按钮和文本字段的表格 当我单击文本字段或行时,我正在创建新单元格
但是当我有不止一行时,当我点击文本字段直接到另一个字段时,它不会将第一响应者分配给文本字段
这是cellForRowAtIndexPath
和textFieldDidEndEditing
的代码
- (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];
wholeSale = [[UITextField alloc] initWithFrame:CGRectMake(240, 0, 100, 32)];
wholeSale.borderStyle = UITextBorderStyleLine;
wholeSale.layer.borderWidth=2.0f;
wholeSale.layer.borderColor=[UIColor colorWithRed:81/255.0 green:105/255.0 blue:169/255.0 alpha:(1)].CGColor;
wholeSale.clipsToBounds=YES;
[wholeSale setFont:[UIFont boldSystemFontOfSize:14]];
[wholeSale setTextAlignment:UITextAlignmentCenter];
[wholeSale setBackgroundColor:[UIColor whiteColor]];
wholeSale.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
wholeSale.placeholder=@"Money";
[wholeSale setDelegate:self];
wholeSale.tag=102;
[cell.contentView addSubview:wholeSale];
cost = [[UITextField alloc] initWithFrame:CGRectMake(338, 0, 100, 32)];
cost.borderStyle = UITextBorderStyleLine;
cost.layer.borderWidth=2.0f;
cost.layer.borderColor=[UIColor colorWithRed:81/255.0 green:105/255.0 blue:169/255.0 alpha:(1)].CGColor;
cost.clipsToBounds=YES;
[cost setFont:[UIFont boldSystemFontOfSize:14]];
[cost setTextAlignment:UITextAlignmentCenter];
[cost setBackgroundColor:[UIColor whiteColor]];
cost.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
cost.placeholder=@"Cost";
[cost setDelegate:self];
cost.tag=103;
[cell.contentView addSubview:cost];
wholeSale=(UITextField*)[cell.contentView viewWithTag:102];
cost=(UITextField*)[cell.contentView viewWithTag:103];
-(void)textFieldDidEndEditing:(UITextField *)textField
// [textField resignFirstResponder];
UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
UITableView *table = (UITableView *)[cell superview];
NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
NSLog(@"%d",textFieldIndexPath.row);
indexPathRow=textFieldIndexPath.row;
Products *record;
if (indexPathRow==[allProdutsData count])
NSManagedObjectContext *context = [appDelegate managedObjectContext]; // get AppDelegate's NSManagedObjectContext
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Products" inManagedObjectContext:context];
record = [[Products alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
NSLog(@"id==%d",[allProdutsData count]);
Products *lastFile=[allProdutsData lastObject];
NSLog(@"%@",[allProdutsData lastObject]);
[record setP_ID:[NSString stringWithFormat:@"%@",lastFile.p_ID]];
NSLog(@"%@",record);
else
NSLog(@"%d",indexPathRow);
// NSLog(@"%@",[allFilesData objectAtIndex:filesDateIndex]);
record=[allProdutsData objectAtIndex:indexPathRow];
NSLog(@"%@",record);
iif (textField.tag==102)
[record setSaleProduct:textField.text];
else if (textField.tag==103)
[record setCostProduct:textField.text];
[self addUpdateProducts:record recordNumber:indexPathRow];
[self animateTextField:textField up:NO];
【问题讨论】:
【参考方案1】:This project 执行与您尝试执行的操作类似的操作。我建议使用它作为示例。
【讨论】:
【参考方案2】:您需要文本字段的委托:
@interface ViewController () <UITextFieldDelegate>
然后试试这个:
@implementation ViewController
- (BOOL)textFieldShouldReturn:(UITextField *)textField
if (textField == self.myTextField)
[self.myTextField resignFirstResponder];
return YES;
【讨论】:
【参考方案3】:我已经找到了解决这个问题的方法
这是由于重新加载数据引起的。
为此,我尝试仅在键盘隐藏时重新加载数据。
在 viewDidLoad 中
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
然后使用以下方法
-(void)reloadData
[_nextAppointmentTableView reloadData];
// [_nextAppointmentTableView reloadData];
[self loadData];
- (void)keyboardDidShow:(NSNotification *)notification
- (void)keyboardWillHide:(NSNotification *)notification
[self performSelector:@selector(reloadData) withObject:nil afterDelay:0.5];
【讨论】:
以上是关于未在具有自定义文本字段的自定义 TableView 中设置第一响应者的主要内容,如果未能解决你的问题,请参考以下文章
具有多行 UILabel 的自定义 Tableview 单元格需要动态高度