UITableView的didSelectRowAtIndexPath方法没有被调用

Xav*_*ero 15 objective-c uitableview ios

我有UIViewController一个UITableView在里面.tableview具有自定义单元格,其中包含文本字段.文本字段的代理设置为该类.

在编辑textfield(textFieldDidBeginEditing)时,我有一个下拉(UIView其中有一个tableview).下拉列表以编程方式创建.我面临的问题是,didSelectRowAtIndexPath没有调用dropdown tableview.我正确地将委托,数据源设置为类.我也删除了所有其他手势.我已确保tableview未处于编辑模式.

在下拉列表中单元格的单击,唯一有效的是我的background tableview的textfield的委托方法.

//UITableView class(with textfield from where dropdown is loaded.)
- (void)textFieldDidBeginEditing:(UITextField *)textField{

if(textField.tag == 3){
    if(dropDown == nil) {
        CGFloat f = 200;
        dropDown = [[SFTextFieldDropDown alloc] showDropDownWithSender:textField withHeight:f andElements:self.list];
        dropDown.delegate = self;
        }
    }
}

// Custom Drop down class.
- (id)showDropDownWithSender:(UITextField *)senderTextField withHeight:(CGFloat)height andElements:(NSArray *)array{

    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
    table.delegate = self;
    table.dataSource = self;
    table.layer.cornerRadius = 5;
    table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
    table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    table.separatorColor = [UIColor grayColor];
    [table setAllowsSelection:YES];
    table.frame = CGRectMake(0, 0, btn.size.width, *height);
    [table setEditing:NO];
    [self addSubview:table];
}
return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.list count];
}


- (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];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}
if([list count] > 0)
    cell.textLabel.text = [list objectAtIndex:indexPath.row];

return cell;
}
Run Code Online (Sandbox Code Playgroud)

小智 1

我认为您的触摸会被下面的单元格接收UITextField,而不是被传递到下面的单元格。您需要做的就是textField.userInteractionEnabled = NO最初设置,以便单元格接收触摸。

然后这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // get the reference to the text field
    textField.userInteractionEnabled = YES;
    [textField becomeFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)