0

I have searched stack overflow (Getting row of UITableView cell on button press) and have found many solutions that people claim to work but I have yet to find the perfect solution. I am trying to get the indexPath of the next/previous UITextField within my UITableView using a CGPoint so that I can scroll to it and make it first responder. Most of the time it is correct however if my text field is a previous field high up in the UITableView in another section I get the wrong result.

Here is the code (Note: nextTxtField is a UIView):

CGPoint point = [nextTxtField convertPoint:CGPointZero toView:tableView];        
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:point];

[UIView animateWithDuration:0.03 delay:0.0 options:0 animations:^{
            [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        } completion:^(BOOL finished) {
            dispatch_async(dispatch_get_main_queue(), ^(void){
                [nextTxtField becomeFirstResponder];
            });
        }];
Community
  • 1
  • 1
HawkEye1194
  • 298
  • 1
  • 3
  • 14

1 Answers1

0

You may get the indexPath of by row/item of UITableView by calling :

[NSIndexPath indexPathForRow:row inSection:section];  
[NSIndexPath indexPathWithIndex:index];  
[NSIndexPath indexPathForItem:item inSection:section];

Once you get the indexPath, you may get the UITableViewCell by:

[_tableView cellForRowAtIndexPath:indexpath];

It returns a UITableViewCell. TypeCast it to your cell class (be sure about it).

Then you may grab hold of nextTxtField as:

[((YourCellClass *) cell).nextTxtField becomeFirstResponder]  

after scrolling UITableView to that indexPath.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
sashi_bhushan
  • 394
  • 3
  • 16
  • I need to get the indexPath if I am only given the table view and the uitextfield. This will not work especially if the UITableViewCell is out of view cellForRowAtIndexPath will return nil because of cell reuse. – HawkEye1194 Aug 25 '16 at 19:05
  • How are you given a uitextfield instance which is contained in uitableviewcell with reuse identifier. How do you grab hold of uitextfield ?? – sashi_bhushan Aug 25 '16 at 19:11
  • Sorry I do not do cell reuse. I allocate the cells because I had to, my table won't be that large. So I have references to the controls in an array. – HawkEye1194 Aug 25 '16 at 19:12
  • With the text field I need to figure out the index path using the convertpoint function. – HawkEye1194 Aug 25 '16 at 19:14
  • If u have all the instances of UITextField that you have added as subView in Cell , i would suggest go up in Hierarchy(call superview) until you find the UITableViewCell Class. Once you get the instance , break the loop scroll to that cell you just got (or take out the indexpath as you prefer) – sashi_bhushan Aug 25 '16 at 19:17
  • How can I get the indexPath from the cell? – HawkEye1194 Aug 25 '16 at 19:19
  • [tableView indexPathForCell : ] – sashi_bhushan Aug 25 '16 at 19:23
  • Thank you. I will try it out and mark your solution as correct once I try it out if it works. – HawkEye1194 Aug 25 '16 at 19:26
  • it works if the cell is not very far. If the cell with the text field is very far, meaning that the table view has to scroll a lot, I get an indexPath with a row and section of 0, which is not correct. Any ideas? – HawkEye1194 Aug 25 '16 at 20:24
  • I mean it returns nil because it is far away. I am not using cell reuse. – HawkEye1194 Aug 25 '16 at 20:33
  • I even tried storing the indexPaths for all of the cells when they are initialized and they get assigned nil automatically if the gap between the current cell and the next cell is very large. Even after creating a copy of the indexPath object and maintaining a strong reference to them. – HawkEye1194 Aug 25 '16 at 21:04
  • So let me assume you have 30 cells . You precisely know which textfield to make first responder, so you can keep an additional metadata in your array where you have reference to uitextfield (use dictionary with row and textfield value and store in array). The metadata would be the indexpath.row when you add the textfield. now instead of calling textfield , take the row no and scroll to that by taking the indexpath out as : – sashi_bhushan Aug 26 '16 at 04:05
  • [NSIndexPath indexPathForRow:row inSection:section]; – sashi_bhushan Aug 26 '16 at 04:05