UITableView的didSelectRowAtIndexPath方法没有被调用

时间:2015-05-20 06:15:25

标签: ios objective-c uitableview

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

在编辑文本字段(textFieldDidBeginEditing)时,我有一个下拉列表(UIView,其中包含一个tableview)。下拉列表以编程方式创建。 我面临的问题是,下拉表视图的didSelectRowAtIndexPath未被调用。我正确地将委托,数据源设置为类。我也删除了所有其他手势。我已确保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;
}

7 个答案:

答案 0 :(得分:3)

从我所看到的你没有填写你的桌面视图,它就像那样简单。

如果你没有给它一个数组而不是调用 - reloadData,那么它永远不会调用委托方法。 (numberOfRowsInSectioncellForRowAtIndexPath

答案 1 :(得分:2)

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

然后这样做:

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

答案 2 :(得分:2)

从我所看到的情况来看,您没有将self.list设置为

中收到的数组
- (id)showDropDownWithSender:(UITextField *)senderTextField 
                  withHeight:(CGFloat *)height 
                 andElements:(NSArray *)array

添加self.list = array,这应该解决它

答案 3 :(得分:2)

因此,在您的问题解释中,您在ViewController的视图和视图中只有两个UITableView。多个UITextFields(如调用dropDown的textField& dropdown本身在每行中都有文本字段。)

问题是,UITableView delegate设置为self(这意味着两个UITableView委托给一个类。所以在收到didSelectRowAtIndexPath委托后tableview 1}}应该被视为回复。为此,您必须向TableView添加标签,例如

#define MAIN_TABLE_VIEW 1001

#define DROP_DOWN_TABLE_VIEW 1002

现在在didSelectRowAtIndexPath中,尝试根据标记值将执行路径重定向到正确的tableView。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tablview.tag == MAIN_TABLE_VIEW){
    // follow your code for main table view here
    }

    if(tablview.tag == DROP_DOWN_TABLE_VIEW){
     // follow your code for dropdown table view here.
    }
}

您可能必须遵循所有delegates& 1 datasource UITableView。{/ p>

希望这能解决目的。

答案 4 :(得分:2)

我认为我遇到了同样的问题,你只在ViewController中声明了UITableViewDelegateUITableViewDataSource

我认为您已经失去了委托,因为您需要并且已将委托分配给TableView内的UIView?对?

在其他视图中显示或调用/加载tableview时重新分配代理

希望这很有用.. :)

答案 5 :(得分:2)

我能够完成与你过去的项目非常相似的事情。我们最终做的是使用textfield.inputView代替tableView这样包含:

enter image description here

答案 6 :(得分:1)

目前还不清楚您要添加下拉子视图的视图。您是在tableView中还是在包含tableView的视图中添加它(顺便说一下,按照您的描述,您没有使用UITableViewController)。我的猜测是你在tableview中添加它并导致问题。尝试将其添加到整个ViewController.view(这意味着您必须获得相对于单元格中textField的框架的正确坐标。)

相关问题