我有静态UItableView,它在行中编辑了UITextFields。我为一个AutoComplete实现了一个UITextField,这很好。当用户键入时,会显示一个新的表视图,当用户键入更多字符时,该表视图会缩小。我非常喜欢这个特性,我想在同一个表的其他三个文本字段中实现它。
想想汽车的制造,型号,车身,颜色。
现在,每当我选择任何文本字段时,我都可以开始键入,然后表视图就会向下钻取,用户可以点击它们的选择。
我遇到的唯一问题是,我不知道如何针对didSelectRowAtIndexPath上第二个表视图中的调用文本字段。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
if (tableView == self.autocompleteTableView) {
// HERE!! What do I compare against to see which textfield should get the autocomplete text?
if (self.txt_model) {
self.txt_model.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}
if (self.txt_make) {
}
if (self.txt_color){
}
if (self.txt_body) {
}
self.hasChanged = YES;
} else {
/// other non important but working stuff
},我与哪个文本字段进行比较,看哪个文本应该得到自动完成的文本?
在实现其他3次演练之前,我的didSelectRowAtIndexPath如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
if (tableView == self.autocompleteTableView) {
self.txt_model.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
} else {
....
}
}所有文本字段的设置如下:
self.txt_model.inputAccessoryView = self.autocompleteTableView;
self.txt_model.delegate = self;
self.txt_make.inputAccessoryView = self.autocompleteTableView;
self.txt_make.delegate = self;
self.txt_body.inputAccessoryView = self.autocompleteTableView;
self.txt_body.delegate = self;
self.txt_color.inputAccessoryView = self.autocompleteTableView;
self.txt_color.delegate = self;一切都正常运作。
发布于 2015-03-04 07:32:13
为此,您需要实现UITextFieldDelegate方法。
需要声明UITextField的全局实例,如下所示:
UITextField *currentTxt;实现textfield委托方法,如:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTxt = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
currentTxt = nil;
}并设置如下数据:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set the data using
currentTxt.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}发布于 2015-03-04 07:44:57
我--你可以用[textField isFirstResponder]来解决你的问题,它可以帮助你决定哪个textField是焦点。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
if (tableView == self.autocompleteTableView) {
if([self.txt_model isFirstResponder]){
self.txt_model.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}
else if([self.txt_make isFirstResponder]){
self.txt_make.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}
else if([self.txt_color isFirstResponder]){
self.txt_color.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}
else if([self.txt_body isFirstResponder]){
self.txt_body.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}
} else {
....
}
}https://stackoverflow.com/questions/28846680
复制相似问题