2

I have composed a tableview with some custom cells. Some of the custom cells contain UIWebView, some has UISlider plus UITextField, some has UIButton.

Now when I click on textfield which is uitableviewcell's subview at the bottom of the UITableView, I can't manage to get my table cell properly visible above the keyboard.

Using of UITableViewController instead of UIViewController is not possible for me. Because my UI structure is somewhat weird and dynamic. In simple way it is like

UIView -> UIScrollView with paging -> x number of UITableViews -> y number of UITableViewCell ->  z number of UITextFields, UISliders, UIWebViews, UIButtons, UILables, etc.

I have also tried below code to make it work.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
    [table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

but it is not working :(

Thanks,

EDIT 1:

I have checked references for textfield, tableviewcell and tableview. So reference of object is not the problem.

Ankur
  • 5,086
  • 19
  • 37
  • 62
  • Why aren't you just resizing the entire UIView, when the keyboard is visible? – Lefteris Mar 28 '13 at 11:08
  • Does textFieldDidBeginEditing:() method is firing or not? – Bhanu Prakash Mar 28 '13 at 11:22
  • @Lefteris: then I have to resize everything in that, I dont think this is good idea. – Ankur Mar 28 '13 at 11:29
  • @BhanuPrakash: yes ofcourse it is firing. – Ankur Mar 28 '13 at 11:29
  • @CrimeMasterGOGO But you are using a UITableView (it doesn't matter that you are nesting it inside a scrollview). So resizing a UITableView, is not requiring you to do anything actually. It will just move the cells up, as you want to do so... – Lefteris Mar 28 '13 at 11:32
  • In textFieldDidBeginEditing() method, print the "cell" and see the result, wheather your getting the exact cell or not? And let us know... – Bhanu Prakash Mar 28 '13 at 11:32
  • @downVoter why downvote... show some guts to put a reason also.... – Ankur Mar 28 '13 at 11:40
  • @Lefteris: hmmm... I can give it a try... – Ankur Mar 28 '13 at 11:43
  • @BhanuPrakash: yes, the same reference.... – Ankur Mar 28 '13 at 11:44
  • @CrimeMasterGOGO - did you checked the cell data by printing in textFieldDidBeginEditing() or not? – Bhanu Prakash Mar 28 '13 at 11:46
  • Now check table_Question[pageNumber] is the exact table where the cell is present or not, by checking the log of pageNumber? Because you have multiple tables in your scenario right? – Bhanu Prakash Mar 28 '13 at 11:48
  • @BhanuPrakash : Reference is not the problem, because I am performing some other operations using these references which works fine. – Ankur Mar 28 '13 at 12:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27089/discussion-between-bhanu-prakash-and-crime-master-gogo) – Bhanu Prakash Mar 28 '13 at 12:13
  • [Have you tried this](http://stackoverflow.com/a/6354426/1756131) – DD_ Mar 28 '13 at 12:30
  • @MicRO - updated my code. And the link which you have posted I saw now thanks...:-) – Bhanu Prakash Mar 28 '13 at 12:38
  • @BhanuPrakash I'm talking to the OP. I didn't try your code – DD_ Mar 28 '13 at 12:45
  • @MicRO : did the trick... thank you so much... can you please post it as an answer so it may help someone else also.. – Ankur Mar 28 '13 at 12:58
  • You can update your question , providing information that you got it solved from the given link. No need to copy paste the same answer here – DD_ Mar 28 '13 at 13:14

3 Answers3

2

Try this code....

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
        UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
        NSIndexPath *indexPath = [[table_Question[pageNumber] indexPathForCell:cell];

        int totalRows = 0;
        if([[table_Question[pageNumber] numberOfSections] > 1)
         {
              for(int i = 0; i<([indexPath.section] - 1);i++)
               {
                     totalRows += [[table_Question[pageNumber] numberOfRowsInSection:i];
                }
          }

          totalRows += indexPath.row;

        int  yPosition = totalRows * cell.frame.size.height;
        CGPoint offset = CGPointMake(0, yPosition);
        [[table_Question[pageNumber] setContentOffset:offset animated:YES];
}

This will helps for you...

Bhanu Prakash
  • 1,493
  • 8
  • 20
1

As per my understanding, you want to show the UITextField as well as the keyboard, when the user starts editing the UITextField inside the UITableViewCell. If that's the case, you can set the contentInset of the UITableView to the required size and then just scroll to the required index path.

Following is a sample code:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    @try
    {
        table_Question[pageNumber].contentInset = UIEdgeInsetsMake(CGFloat Req_top, CGFloat Req_left, CGFloat Req_bottom, CGFloat Req_right);
        [table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];

        //Add this to you code to scroll in the tableview gets hidden by keyboard
        CGPoint scrollPoint = CGPointMake(0, Required_Height);
       [yourScrollView setContentOffset:scrollPoint animated:YES];
    }
}
Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
Krishna
  • 293
  • 3
  • 11
  • Where is the position of your tableview. If its at the bottom of the screen OR somewhere where the keyboard covers its view then you have to increase the "contentSize" of the scroll view also. Add this to your textfieldshouldBeginEditing CGPoint scrollPoint = CGPointMake(0,Required_Height); [your_scrollview setContentOffset:scrollPoint animated:YES];' – Krishna Mar 28 '13 at 13:53
1

Try this code

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    UITableViewCell *aCell = [table_Question[pageNumber] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    CGSize cellSize = aCell.frame.size;
    [table_Question[pageNumber] setContentOffset:CGPointMake(0, cellSize.height*2) animated:YES];
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71