Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 494
Xavi Valero Avatar asked May 20 '15 06:05

Xavi Valero


4 Answers

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)

like image 95
Gil Sand Avatar answered Nov 11 '22 07:11

Gil Sand


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.

like image 24
Balram Tiwari Avatar answered Nov 11 '22 08:11

Balram Tiwari


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

like image 31
Juan Avatar answered Nov 11 '22 08:11

Juan


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.. :)

like image 25
0yeoj Avatar answered Nov 11 '22 07:11

0yeoj