0

I Have a UITableView with custom cell with UITextField and buttons on that ,my issue is when ever the user selects some textfields in bottom keybord is hiding that textfield , i tried to scroll up the UITableView by seeing some answers in stackoverflow. but it is not scrolling can any one help me in finding out the mistake made by me please.i have written code for scrolling in textFieldDidBeginEditing: method .textFieldDidBeginEditing: is also firing and executing the code in that but it is not scrolling up.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
   // NSLog(@"No OF rows:%d",[contents count]);
return [contents count];

}

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

static NSString *cellIdentifier = @"cell";

// Try to retrieve from the table view a now-unused cell with the given identifier.
cell = (uploadCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"uploadCustomCell"];
if (cell == nil) {
    cell = [[uploadCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"uploadCustomCell"];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"uploadCustomCell"
                                                        owner:self options:nil];
          cell = [nib objectAtIndex:0];
}


saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
dictionaryContents = [contents objectAtIndex:indexPath.row];
cell
.nameLabelCell.text   = [dictionaryContents valueForKey:@"VideoName"];
cell.userName.text = [dictionaryContents valueForKey:@"User"];
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
   NSString *defaultVideo = [dictionaryContents valueForKey:@"DefaultVideo"];
if ([defaultVideo isEqual: @"1"]) {
    [defaultSwitche setOn:YES animated:YES];

}
else{
    [defaultSwitche setOn:NO animated:YES];
}


[cell.defaultSwitch addTarget:self action:@selector(setState:)forControlEvents:UIControlEventValueChanged];
VideoNameTextField.hidden = YES;
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 207;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    selectedRow=indexPath.row;
indexpathTest = indexPath.row;
[tabelView1 reloadData];
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
guid = [dictionary valueForKey:@"GUID"];
detailsVehImg.image = [arrayimage objectAtIndex:indexPath.row];;


}
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath  *)indexPath
 {
  return YES;
 }
  - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  return  UITableViewCellEditingStyleDelete;

}

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



[self.tabelView1 scrollToRowAtIndexPath:1 atScrollPosition:UITableViewScrollPositionTop animated:YES];


}
Yashesh
  • 1,799
  • 1
  • 11
  • 29
KMKR
  • 109
  • 1
  • 12

3 Answers3

0

You should listen to the keyboard notifications and change your table view frame to show the cell you want. Scrolling isn't enough as if the cell is at the bottom it will be hidden anyway

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillDisappear:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

-(void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)keyboardWillAppear:(NSNotification *)note
{
    CGRect tableViewFrame = self.tableView.frame;
    // If your table view doesn't end at the bottom of the screen then this calculation of the height wouldn't be enough, you would need to take into consideration the distance between the screen bottom and the table view
    tableViewFrame.size.height = tableViewFrame.size.height - [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    CGRect visibleFrame = CGRectZero;
    visibleFrame.origin = self.tableView.contentOffset;
    visibleFrame.size = tableViewFrame.size;

    [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                     animations:^{
                         self.tableView.frame = tableViewFrame;
                     }
                     completion:^(BOOL finished){
                         [self.tableView scrollRectToVisible:visibleFrame
                                                    animated:YES];
        }];
}

-(void)keyboardWillDisappear:(NSNotification *)note
{
    CGRect tableViewFrame = self.tableView.frame;
    tableViewFrame.size.height = tableViewFrame.size.height + [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState | [[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                     animations:^{
                         self.tableView.frame = tableViewFrame;
                     }
                     completion:nil];
}
Moxy
  • 4,162
  • 2
  • 30
  • 49
  • when i'm giving this code in completion block // NSArray *indexPathsForVisibleRows = [self.tabelView1 indexPathsForVisibleRows]; // NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count/2)]; // [self.tabelView1 scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; i'm getting incompatible block pointer types sending 'void (^)(void)' to parameter of type 'void (^)(bool)' error – KMKR May 20 '13 at 09:38
  • i'm getting this error i'm getting incompatible block pointer types sending 'void (^)(void)' to parameter of type 'void (^)(bool)' error – KMKR May 20 '13 at 09:52
  • Sorry there was an error it should be completion:^(BOOL finished){} – Moxy May 20 '13 at 09:56
  • it is moving up but, here my table view is in little part only it is crossing the view and moving out of the view. – KMKR May 20 '13 at 10:05
  • What's your table view frame? – Moxy May 20 '13 at 10:10
  • it seams like here table view is not scrolling total view is moving up. – KMKR May 20 '13 at 10:11
  • Try the new -keyboardWillAppear: – Moxy May 20 '13 at 10:27
  • 1
    It's a shame you don't have enough reputation to discuss it over chat. Anyway, scrolling alone won't help you as I already said: If the cell is at the bottom of the table view it won't be visible. the solution is to make the table view short or move it up then scroll to make the cell you want visible. It depends a lot on your layout so you should experiment if the result of my solution doesn't solve your case. – Moxy May 20 '13 at 10:46
0

Try this

[self.myTableView setContentOffset:CGPointZero animated:YES];

OR

// Table View - Scroll to top
[self.myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self.myTableView reloadData];

Parameter
animated:YES show scrolling animation.
animated:NO display table after scrolling(no animation.)

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
-2

You have to reload your tableView before scrolling it. I have added one line of code in your textFieldDidBeginEditing. PLease try this.

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{
    [self.tabelView1 reloadData];

    NSArray *indexPathsForVisibleRows = [self.tabelView1 indexPathsForVisibleRows];
    NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count/2)];
    [self.tabelView1 scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

}

Good Luck

Ajay Chaudhary
  • 1,993
  • 15
  • 23
  • Probably the cell he's trying to show is at the bottom of the tableview so scrolling wouldn't work! – Moxy May 20 '13 at 09:12