首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UITableView on didSelectRowAtIndexPath中选择正确的didSelectRowAtIndexPath

在UITableView on didSelectRowAtIndexPath中选择正确的didSelectRowAtIndexPath
EN

Stack Overflow用户
提问于 2015-03-04 04:27:53
回答 2查看 723关注 0票数 0

我有静态UItableView,它在行中编辑了UITextFields。我为一个AutoComplete实现了一个UITextField,这很好。当用户键入时,会显示一个新的表视图,当用户键入更多字符时,该表视图会缩小。我非常喜欢这个特性,我想在同一个表的其他三个文本字段中实现它。

想想汽车的制造,型号,车身,颜色。

现在,每当我选择任何文本字段时,我都可以开始键入,然后表视图就会向下钻取,用户可以点击它们的选择。

我遇到的唯一问题是,我不知道如何针对didSelectRowAtIndexPath上第二个表视图中的调用文本字段。

代码语言:javascript
复制
- (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如下所示:

代码语言:javascript
复制
- (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 {
        ....
        }
    }

所有文本字段的设置如下:

代码语言:javascript
复制
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;

一切都正常运作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-04 07:32:13

为此,您需要实现UITextFieldDelegate方法。

需要声明UITextField的全局实例,如下所示:

代码语言:javascript
复制
UITextField *currentTxt;

实现textfield委托方法,如:

代码语言:javascript
复制
- (void)textFieldDidBeginEditing:(UITextField *)textField
 {
    currentTxt = textField;
 }

 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
    currentTxt = nil;
 }

并设置如下数据:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Set the data using
   currentTxt.text = [self.arrAutoComplete objectAtIndex:indexPath.row];
}
票数 0
EN

Stack Overflow用户

发布于 2015-03-04 07:44:57

我--你可以用[textField isFirstResponder]来解决你的问题,它可以帮助你决定哪个textField是焦点。

代码语言:javascript
复制
- (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 {
        ....
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28846680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档