Janani
Janani

Reputation: 51

Implementing previous/next buttons in a tableview with varied controls( Buttons, switches, and textfields)

I have a scenario where I have to implement previous/next buttons in a tableview. The table is of custom cells. Some of them are a drop down, some switches and some textfields. It consists of multiple sections and rows.

I need to jump from one textfield to the next textfield on clicking next button.

I can split my problem statement into: 1. When a next button is pressed on a textfield, finding the next consecutive textfield in the table. 2. Scrolling to the nextfield. 3. Making it the first responder.

I have tried a lot of techniques avaliable online, with no luck. Also, any other suggestions would help on how to go ahead implementing such a functionality

            NSMutableArray *cells = [[NSMutableArray alloc] initWithArray:[tableView visibleCells]];
            int count1 = 0;
           //tableViewCell_Ncolumn is a custome cell
            for (tableViewCell_Ncolumn *cell in cells)
            {
                count1 ++;
                 for (UIView *view in  cell.contentView.subviews)
                 {
                     if ([view isKindOfClass:[UITextField class]])
                     {

                           UITextField* currField = (UITextField *)view;
                            for (int i=count1; i<[cells count]; i++)
                            {
                                UITextField *nextField;
                                 //Geting current textfield and checking if it is being edited
                                 if ([currField isEditing])
                                      {
                                          tableViewCell_Ncolumn *nextCell = [cells objectAtIndex:i+1];
                                          for (UIView *view in  cell.contentView.subviews)
                                          {
                                              if ([view isKindOfClass:[UITextField class]])
                                              {
                                                  nextField = (UITextField *)view;
                                              }
                                              else
                                              {
                                                  //loop till you find next textfield in the table

                                              }
                                          }


                                          [nextField becomeFirstResponder];

                                          [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i+1 inSection:1] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

                                          break;


                                      }

                            }

                     }
                 }
            }
        };

Upvotes: 0

Views: 701

Answers (3)

jday
jday

Reputation: 578

I had a similar issue and ended up creating a subclass of UIToolbar to manage the next/previous functionality: https://github.com/jday001/DataEntryToolbar

You make the toolbar the inputAccessoryView of your text fields and add them to it's dictionary. This allows you to cycle through them forwards and backwards, even with dynamic content. There is an example app to help with the implementation details. You will need your own data model to keep track of the values inside the fields.

DataEntryToolbar is written in swift, but you can mix it in with your objective-c code, no problem. If you run into any issues with that, check apple's docs here under Importing Swift into Objective-C: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Edit: Just realized this post is almost a year old, but hopefully this will help someone else.

Upvotes: 0

bhavya kothari
bhavya kothari

Reputation: 7474

For textfields you can use below approach and workarounds for other controls can be developed by this appraoch.


textFieldShouldBeginEditing - set scroll position
actionNext - set first responder to next textfield resign current
actionPrevious - set first responder to prev textfield resign current
actionDone - resign first responder

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if (textField.tag == 25)
    {

        [tableView setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES];

      // or you can use this method as well      
      // [[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

   return YES;

    }
 }

- (IBAction)actionPrevious:(id)sender {

    if ([self.textFieldEmployer1 isFirstResponder])
    {
       [self.textFieldConfirmPassword becomeFirstResponder];
    }
    else if ([self.textFieldConfirmPassword isFirstResponder])
    {
       [self.textFieldPassword becomeFirstResponder];
    }

 }

  - (IBAction)actionNext:(id)sender {

    if ([self.textFieldEmail isFirstResponder])
    {
        [self.textFieldPassword becomeFirstResponder];
    }
   else if ([self.textFieldUserName isFirstResponder])
   {
       [self.textFieldPassword becomeFirstResponder];
   }

}

- (IBAction)actionDone:(id)sender
{

[self.view endEditing:YES];

}

Upvotes: 1

Sunny Shah
Sunny Shah

Reputation: 13020

Best way is to use give tag property.

 UITextField* currField=[cell.contentView viewWithTag:5];
 UITextField* nextField =[cell.contentView viewWithTag:6];

     [currField resignFirstResponder];
     [nextField becomeFirstResponder];

Upvotes: 0

Related Questions