0

I have a UITableViewController with custom UITableViewCell. Where each cell contains a UITextField. Now what i want is to shift the control from one text field to the every next text field when return key is pressed. what i m thinging to do is this...

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSIndexPath *currentIndexPath=[self.tableView indexPathForCell:(UITableViewCell*)textField.superview.superview];
    if(currentIndexPath.row<_excerciseData.totalBlanks-1){
        NSIndexPath nextIndexPath=???
        UITextField *nextTextFeild=[(UITableViewCell*)[self.tableView cellForRowAtIndexPath:nextIndexPath] viewWithTag:2];
        [nextTextFeild becomeFirstResponder];
    }else{

[textField resignFirstResponder];
}
return YES;
}

but for this i don't know, how to find the indexPath of very next UITableViewCell. Can anybody help me to find this? this... :)

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
Dipika
  • 1,097
  • 1
  • 9
  • 19
  • check this.....http://stackoverflow.com/questions/2825388/iphone-jump-to-next-uitextfield-in-uitableview-how-to – Bhavin Bhadani Oct 14 '15 at 12:36
  • Definitely don't use `.superview.superview` – Wain Oct 14 '15 at 12:43
  • @Wain why not to use this ... can u explain? – Dipika Oct 14 '15 at 12:49
  • @Wain is correct. because textField.superview.superview will not get the tableviewcell for some of the iOS versions. – Venk Oct 14 '15 at 12:53
  • 1
    The view hierarchy may change during the versions of the OS. Avoid typecasting the views as you are not sure what may happen later. Also you may find yourself in the position where you will add the field to a new subview and this code will need to change. If you must do what you did at least try it like so: UITableViewCell *cell = myTextField; while ([cell isKindOfClass:[UITableViewCell class]] == NO) { cell = cell.superview; if(cell == nil) { break; } } – Matic Oblak Oct 14 '15 at 12:54
  • Also don't use `== NO`, just use `!` (true and false are not always absolutes and it's easy to get into bad habits) – Wain Oct 14 '15 at 14:18
  • thank you very much Wain, Matic and Venkat ... as i m new to iOS, i don't have knowledge about these scenarios so, thxs again.... – Dipika Oct 15 '15 at 05:07
  • @Wain it is actually the opposite. You should avoid using negations for both boolean values and pointers. Not all systems ensure that zero value is any of FALSE, NO, NULL... That is the reason you have type defines for these values. It is the same for now working iOS and I think it will stay the same but unfortunately as is with Apple you never know. Just look at what they did with CGFloat, NSInteger and such... – Matic Oblak Oct 15 '15 at 06:35

4 Answers4

0

Since you already have a custom table view cell the simplest solution would be to add a property that holds the index path. Then in the delegate method where you return the cell simply assign the current index path to the cell which may later be used.

This will not work very well for the last cell in the section or the table view itself. What you should do is report the event to the table view owner (the view controller) from the cell itself which may then compute what the next cell index is: If you have multiple sections it must check if it should go to a new section and if it is the last cell simply do nothing (or go to the beginning of the table view if you wish).

For forwarding messages you may try one of the procedures mentioned in the answer here: IOS: Navigate through tableView and show table view data on next view

Just to be clear, the text field delegate should be the cell itself!

Community
  • 1
  • 1
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • according to ur solution if i assign the current index path to a indexPath property in the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { } delegate then, at last it will contain the index path of the last visible cell ... 1. is m right? – Dipika Oct 15 '15 at 05:27
  • and if yes this index path i don't need .... i actually need the selected cell's index path – Dipika Oct 15 '15 at 05:32
  • No, the cell itself holds the index path which means you may have many at the same time. Since the text field delegate will report to the cell itself this cell will have the correct index path... – Matic Oblak Oct 15 '15 at 05:38
  • You must call cell.indexPath = indexPath. And NOT self.indexPath = indexPath in the cellForRow method. – Matic Oblak Oct 15 '15 at 05:39
0

Better you can do like this,

CGPoint pointInTable = [field convertPoint:field.bounds.origin toView:_tableView];    

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];

Reference : How to get a UITableViewCell from one of its subviews

or do like this,

id view = [textfield superview];

while (view && [view isKindOfClass:[UITableViewCell class]] == NO) {
    view = [view superview]; 
}

UITableViewCell *tableViewCell = (UITableViewCell *)view;

Reference : How to get UITableView from UITableViewCell?

Community
  • 1
  • 1
Venk
  • 5,949
  • 9
  • 41
  • 52
0

Better way you can use tag property of textField. Do like this.

While you creating cell with cellForRowAtIndexPath give a tag value for textField inside the cell like:

cell.yourTextField.tag = indexPath.row + someConstant; // someConstant is just to make sure that there is no textfield with the same tag value

and in your textField delegate: - (BOOL)textFieldShouldReturn:(UITextField *)textField implement the following.

// Jumping with the keyboard Next button implementation.
if (UIReturnKeyNext == textField.returnKeyType) //may be u can avoid this check
{
    id nextField = [self viewWithTag:textField.tag + 1];
    if ([nextField isKindOfClass:[UITextField class]])
    {
        [nextField becomeFirstResponder];
    }
}

Hopes the textFieldDelegates are implemented in the tableview's owner class.

Alex Andrews
  • 1,498
  • 2
  • 19
  • 33
0

Solution here

#prgma mark - UITableViewDelegate Methods

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

   static NSString *stringCell = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   if (cell == nil)
   {
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
   }
  cell.yourTextField.tag = indexPath.row;
  return cell;
}

#prgma mark - UITextFieldDelegate Methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
     NSUInteger index = textField.tag;
     UITextField *nextTextField = (UITextField*)[self.view viewWithTag:index+1];
     [nextTextField becomeFirstResponder];
     return YES;
}
user3182143
  • 9,459
  • 3
  • 32
  • 39