ios - UITableView 的 didSelectRowAtIndexPath 方法未被调用

标签 ios objective-c uitableview

我有一个UIViewController,里面有一个UITableView。 tableview 有自定义的单元格,里面有文本字段。文本字段的代表被设置为该类。

在编辑文本字段时(textFieldDidBeginEditing),我有一个下拉菜单(UIView,里面有一个 tableview)。下拉列表以编程方式创建。 我面临的问题是,没有调用下拉 ListView 的didSelectRowAtIndexPath。我已经正确地将委托(delegate)、数据源设置为类。我也删除了所有其他手势。我已确保 tableview 未处于编辑模式。

在单击下拉列表中的单元格时,唯一起作用的是我的背景 TableView 的文本字段的委托(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/

相关文章:

ios - 向 uiview 添加从 A 点移动到 B 点的动画

ios - 按月将带有 NSDate 对象的 NSArray 排序到 NSDictionary

ios - TabBarController 中 UITableView 无法识别的选择器错误

ios - 如何解决UITextView中的 "auto-scroll"?

ios - 如何在 cocos2d 中创建带编号的有序文本?

objective-c - 如何编写在 Objective-C 中调用 `super` 实现的 c 函数?

objective-c - 保留 kPasteboardTypeFileURLPromise 时的错误处理

ios - heightForRowAt 与 estimatedHeightForRowAt 有什么区别?

iphone - 如何分别创建UITableView和detail view

ios - Spritekit 中的击中动画