Xavi Valero
Xavi Valero

Reputation: 2057

UITableView's didSelectRowAtIndexPath method not getting called

I have aUIViewController with aUITableView in it. The tableview has customized cells, with textfields inside it. The delegates of the textfield are set to that class.

On edit of a textfield(textFieldDidBeginEditing) I have a drop down(UIViewwith a tableview in it). The dropdown is created programmatically. The problem I am facing is, thedidSelectRowAtIndexPath of the dropdown tableview isn't being called. I have set the delegate, datasource to the class rightly. I have removed all other gestures too. I have made sure that the tableview isn't in editing mode.

On the clicks of cells in the dropdown, the only thing that works is my background tableview's textfield's delegate methods.

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

Upvotes: 15

Views: 1891

Answers (7)

cbiggin
cbiggin

Reputation: 2152

It's unclear which view you're adding your dropdown subview in. Are you adding it in the tableView or in the view that contains the tableView (btw, by your description, you ARE NOT using a UITableViewController). My guess is that you're adding it in the tableview and that is causing problems. Try adding it to the overall ViewController.view (which means you will have to get the proper co-ordinates of the frame relative to the textField within the cell).

Upvotes: 1

Malik Boy
Malik Boy

Reputation: 132

I think your touches are received by UITextField and not being passed to the below cell. What you have to do is set textField.userInteractionEnabled = NO initially so that touches are received by cell.

And then do this :

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

Upvotes: 1

rmp
rmp

Reputation: 3523

I was able to accomplish something very similar to what you are trying to do for a past project. What we ended up doing was using the textfield.inputView instead to contain the tableView like this:

enter image description here

Upvotes: 2

0yeoj
0yeoj

Reputation: 4550

i think i had the same problem, where have you declared the UITableViewDelegate and UITableViewDataSource, just in ViewController only?

I think you have lost the delegate, because you need and did assigned the delegate to the TableView inside the UIView? Right?

Reassign the delegates upon showing or calling/loading tableview from another view..

Hope this is useful.. :)

Upvotes: 2

Juan
Juan

Reputation: 402

From what I can see, you are not setting self.list to the array you receive in

- (id)showDropDownWithSender:(UITextField *)senderTextField 
                  withHeight:(CGFloat *)height 
                 andElements:(NSArray *)array

Add self.list = array and that should fix it

Upvotes: 2

Balram Tiwari
Balram Tiwari

Reputation: 5667

So with your question explanation, you have totally two UITableView in your ViewController's view & multiple UITextFields ( like textField which invokes the dropDown & the dropdown itself has textfields in each row.)

So the problem is, the UITableView delegate is set to the self ( that means two UITableView are delegating to one class. So upon receiving didSelectRowAtIndexPath delegate which tableview should be considered to respond back. For this you have to add tags to the TableView, like

#define MAIN_TABLE_VIEW 1001

#define DROP_DOWN_TABLE_VIEW 1002

Now in your didSelectRowAtIndexPath, try to re-direct your path of execution to the right tableView based on tag values.

- (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.
    }
}

The same approach you may have to follow in all delegates & 1datasource of UITableView.

Hope this solves the purpose.

Upvotes: 2

Gil Sand
Gil Sand

Reputation: 6038

From what I can see you're not populating your tableview, it's as simple as that.

If you're not giving it an array and not calling -reloadData, then it'll never call the delegate methods. (numberOfRowsInSection and cellForRowAtIndexPath)

Upvotes: 3

Related Questions