Mac_Play
Mac_Play

Reputation: 302

Calling uitableview while clicking uitextfield-Data not displaying -iOS

I want to display data in uitableview when the particular uitextfield is tapped. I am not getting the data in taleview. Please check the below and help me.

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"CellIdentifier";



     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier] ;



    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [cell.textLabel setFont:[UIFont fontWithName:@"Bold" size:20]];


     if (ethnicityField.tag == 1)
    {
        cell.textLabel.text = [data objectAtIndex:indexPath.row];

    }

 if (languageField.tag == 2)
    {
         cell.textLabel.text = [langData objectAtIndex:indexPath.row];



    }

Here, when I click on the first textfield it shows the empty table and crashes. It's not taking the exact tag value. Please help.

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
    int row = 0;
    if (ethnicityField.tag == 1)
    {
        row = [data count];
    }
    if (languageField.tag == 2)
    {
         row =   [langData  count];
    }
    return row;
}

Crash report:
Actually I have 2 arrays, one with 5 values and another with 11 values, the problem is both textfield takes first array and if I select the 5th object in second textfield it shows the following error.

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 9 beyond bounds [0 .. 4]'

Upvotes: 1

Views: 408

Answers (1)

streem
streem

Reputation: 9144

I believe ethnicityField and languageField always have the same tag so numberOfRowsInSection always returns [data count];

What you could do is checking whether a textfield is active when you display your table.

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
    int row = 0;
    if ([ethnicityField isFirstResponder])
    {
        row = [data count];
    }
    else if ([languageField isFirstResponder])
    {
         row =   [langData  count];
    }
    return row;
}

Upvotes: 1

Related Questions