如何使用文本字段处理表格视图单元格
Posted
技术标签:
【中文标题】如何使用文本字段处理表格视图单元格【英文标题】:How to handle Table View cell with textfield 【发布时间】:2015-09-10 05:43:37 【问题描述】:我的表格视图单元格包含一个标签和一个文本字段,我正在重复使用我的单元格并且它正在工作。但是当我在第一个文本字段中输入文本并按回车时,我想转到下一个文本字段,我该如何处理?
【问题讨论】:
按下第一个文本字段的返回键后,将您的下一个文本字段设为第一响应者 引用此***.com/questions/4375442/… 【参考方案1】:这就是我所做的:
使用这些方法创建了一个委托协议:
- (void)textFieldInCellDidReturn:(UITableViewCell*)cell;
- (void)textFieldInCellDidBeginEditing:(UITableViewCell*)cell;
- (void)textFieldInCellDidEndEditing:(UITableViewCell*)cell;
在单元格中:
-(BOOL)becomeFirstResponder
return [self.inputTextField becomeFirstResponder];
-(void)textFieldDidBeginEditing:(UITextField *)textField
if ([self.delegate respondsToSelector:@selector(textFieldInCellDidBeginEditing:)])
[self.delegate textFieldInCellDidBeginEditing:self];
-(void)textFieldDidEndEditing:(UITextField *)textField
if ([self.delegate respondsToSelector:@selector(textFieldInCellDidEndEditing:)])
[self.delegate textFieldInCellDidEndEditing:self];
-(BOOL)textFieldShouldReturn:(UITextField *)textField
if ([self.delegate respondsToSelector:@selector(textFieldInCellDidReturn:)])
[self.delegate textFieldInCellDidReturn:self];
return NO;
在视图控制器和表视图委托中,声明 iVar NSIndexPath* indexPathOfFirstResponder
并采用委托
- (void)textFieldInCellDidBeginEditing:(UITableViewCell *)cell
indexPathOfFirstResponder = [self.theTable indexPathForCell:cell];
-(void)textFieldInCellDidReturn:(UITableViewCell *)cell
NSIndexPath* indexPathForCell = [self.theTable indexPathForCell:cell];
if(indexPathForCell.row < [self.theTable numberOfRowsInSection:indexPathForCell.section]-1)
NSIndexPath* nextIndexPathInSection = [NSIndexPath indexPathForRow:indexPathForCell.row+1 inSection:indexPathForCell.section];
UITableViewCell* nextCellInSection = [self.theTable cellForRowAtIndexPath:nextIndexPathInSection];
if (nextCellInSection)
[nextCellInSection becomeFirstResponder];
else
indexPathOfFirstResponder = nextIndexPathInSection;
[self.theTable scrollToRowAtIndexPath:nextIndexPathInSection atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
else
[self.theTable endEditing:YES];
-(void)textFieldInCellDidEndEditing:(UITableViewCell *)cell
indexPathOfFirstResponder = nil;
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
if ([indexPath isEqual:indexPathOfFirstResponder])
[cell becomeFirstResponder];
【讨论】:
【参考方案2】:为 textField 设置顺序标签,例如0,1,2…… 导入 textField 委托方法 textFieldDidEndEditing。每当按下返回键时,都会调用此方法。
使用这样的东西:
-(void)textFieldDidEndEditing:(UITextField *)textField
if(textField.tag == currentTextFieldTag+1)
[textField becomesFirstResponder];
【讨论】:
【参考方案3】:在行函数的单元格中使用此代码
cell.textField.tag = indexPath.row;
现在实现一个UITextFieldDelegate
方法
-(void)textFieldDidEndEditing:(UITextField *)textField
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag+1 inSection:YOUR_SECTION];
UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath];
[cell.textField becomesFirstResponder];
// if you need [yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
我希望这会有所帮助。谢谢
【讨论】:
以上是关于如何使用文本字段处理表格视图单元格的主要内容,如果未能解决你的问题,请参考以下文章
如何在表格视图单元格中添加 12 个文本字段并通过其标记值访问每个文本字段的文本?