我有一个带有UIViewControllerUITableView。表格 View 具有自定义的单元格,其中包含文本字段。文本字段的委托(delegate)设置为该类。

在编辑文本字段(textFieldDidBeginEditing)时,我有一个下拉菜单(UIView中有一个表格 View )。下拉列表是通过程序创建的。
我面临的问题是,下拉列表 View 的didSelectRowAtIndexPath没有被调用。我已经正确地将委托(delegate),数据源设置为该类。我也删除了所有其他手势。我已确保tableview不在编辑模式下。

单击下拉列表中的单元格时,唯一起作用的是我的背景表 View 的文本字段的委托(delegate)方法。

//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;
}

最佳答案

从我所看到的来看,您没有填充表格 View ,就这么简单。

如果您没有给它一个数组并且没有调用-reloadData,那么它将永远不会调用委托(delegate)方法。 (numberOfRowsInSectioncellForRowAtIndexPath)

关于ios - UITableView的didSelectRowAtIndexPath方法没有被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30341649/

10-12 14:46
查看更多